-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (43 loc) · 1.44 KB
/
server.js
File metadata and controls
51 lines (43 loc) · 1.44 KB
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
// server.js
const next = require("next");
const app = next({ dev: process.env.NODE_ENV !== "production" });
const handler = app.getRequestHandler();
// With express
const express = require("express");
// With Express Peer Server
const ExpressPeerServer = require("peer").ExpressPeerServer;
app.prepare().then(() => {
var expressApp = express();
var server = require("http").createServer(expressApp);
// Signalling server
var expressPeerServer = ExpressPeerServer(server, {
allow_discovery: true,
debug: true
});
expressApp.use("/peerjs", expressPeerServer);
var connected = [];
expressPeerServer.on("connection", function (id) {
var idx = connected.indexOf(id); // only add id if it's not in the list yet
if (idx === -1) {
connected.push(id);
}
});
expressPeerServer.on("disconnect", function (id) {
var idx = connected.indexOf(id); // only attempt to remove id if it's in the list
if (idx !== -1) {
connected.splice(idx, 1);
}
});
expressApp.get("/peerjs/:channel", function (req, res) {
var channel = req.params.channel;
return res.json(
connected.filter(item => {
var ch = item.split("-");
return ch.length === 2 && ch[0] === channel;
})
);
});
console.log("listening on port 3000");
expressApp.use(handler);
server.listen(3000);
});