-
Notifications
You must be signed in to change notification settings - Fork 6
/
rtc.js
39 lines (32 loc) · 883 Bytes
/
rtc.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
module.exports = function (opts) {
const WebSocketServer = require("ws").Server;
const wss = new WebSocketServer(opts);
const board = require("rtc-switch")();
let connections = [];
wss.on("connection", function connection(ws) {
const peer = board.connect();
// add the socket to the connection list
connections.push(ws);
ws.on("message", peer.process);
peer.on("data", function (data) {
if (ws.readyState === 1) {
ws.send(data);
}
});
ws.on("close", function () {
// trigger the peer leave
peer.leave();
// splice out the connection
connections = connections.filter(function (conn) {
return conn !== ws;
});
});
});
// add a reset helper
board.reset = function () {
connections.splice(0).forEach(function (conn) {
conn.close();
});
};
return board;
};