-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
183 lines (159 loc) · 4.12 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const express = require("express")
const app = express()
const http = require("http")
const server = new http.Server(app)
const io = require("socket.io")(server)
const helmet = require("helmet")
const cors = require("cors")
const {
rooms,
addSocketToRoom,
removeSocketFromRoom,
allSocketsForRoom,
} = require("./rooms")
const config = require("./config")
const CONFIG = {
title: config.title,
host: process.env.HOST || config.host || undefined,
port: process.env.PORT || config.port || 4444,
timeout: config.timeout || 30000,
max: config.max || 50,
debug: config.debug || false,
}
process.title = CONFIG.title
const log = require("debug")("signal:server")
app.use(helmet())
app.use(cors())
// SOCKET.IO
let brokenSockets = {}
function activeSockets(id = null) {
return Object.keys(io.sockets.connected).filter(
(sid) => sid !== id && !brokenSockets[sid]
)
}
function brokenSocket(socket) {
brokenSockets[socket.id] = true
// log('--- broken sockets', Object.keys(brokenSockets).length, 'connected', activeSockets().length)
io.emit("remove", { id: socket.id })
}
function socketByID(id) {
return io.sockets.connected[id]
}
function emitByID(id, name, msg) {
let socket = socketByID(id)
if (socket) {
log("emit", id, name, msg)
socket.emit(name, msg)
}
}
function broadcastByID(ids, name, msg) {
for (let id of ids) {
emitByID(id, name, msg)
}
}
io.on("connection", function (socket) {
const sid = socket.id
let currentRoom
// let peers = activeSockets(sid)
log("connection socket id:", sid)
for (const msg of ["disconnect", "disconnecting", "error"]) {
socket.on(msg, (data) => {
log(`* ${msg}:`, data)
brokenSocket(socket)
removeSocketFromRoom(sid, currentRoom)
})
}
socket.on("status", (info, cb) => {
log("status", info, cb)
if (cb)
cb({
api: 1,
pong: info?.ping || "pong",
config: CONFIG,
})
})
// The peer that joined is responsible for initiating WebRTC connections
socket.on("join", ({ room }) => {
let peers = allSocketsForRoom(room)
const full = peers.length >= config.max
if (full) {
socket.emit("error", {
error: `Room ${room} is full`,
code: 1,
full,
})
} else {
removeSocketFromRoom(sid, currentRoom)
addSocketToRoom(sid, room)
currentRoom = room
socket.emit("joined", {
room,
peers,
})
}
})
// Ask for a connection to another socket via ID
socket.on("signal", (data) => {
log("signal", data.from, data.to)
if (data.from !== sid) {
log("*** error, wrong from", data.from)
}
if (data.to) {
const toSocket = socketByID(data.to)
if (toSocket) {
toSocket.emit("signal", {
...data,
// from: socket.id,
})
} else {
log("Cannot find socket for %s", data.to)
}
}
})
})
// EXPRESS.IO
const startDate = new Date()
if (CONFIG.debug) {
app.use("/status", (req, res) => {
let status = {
api: 1,
success: true,
info: {
timeStarted: Math.round(startDate.getTime()),
activeConnections: activeSockets().length,
rooms,
},
}
res.json(status)
})
}
app.use("/", (req, res) => {
res.send(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui, viewport-fit=cover">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<title>Briefing Signal Server</title>
</head>
<body>
<p><b><a href="https://brie.fi/ng">Briefing</a> Signal Server</b></p>
<p>Running since ${startDate.toISOString()}</p>
</body>
</html>`)
})
// app.use('/', express.static('public'))
//
server.listen(
{
host: CONFIG.host,
port: CONFIG.port,
},
(info) => {
console.info(`Running on`, server.address())
}
)