-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
154 lines (121 loc) · 3.29 KB
/
index.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
var express = require("express");
const session = require("express-session");
var browserify = require("browserify-middleware");
const uuid = require("uuid");
var bodyParser = require("body-parser");
var http = require("http");
var websocketServer = require("ws");
var cors = require("cors");
var helmet = require("helmet");
var fs = require("fs");
var port = process.env.PORT || 8383;
var hostname = "0.0.0.0";
var channels = {};
var users = {};
var sockets = {};
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(cors());
app.use(helmet());
var server = http.createServer(app);
const wss = new websocketServer.Server({
path: "/wss",
server,
});
app.get("/bundle.js", browserify(["web-audio-stream/writable"]));
app.get("/login", function (req, res) {
const id = uuid.v4();
while (id == undefined) {
// sometimes id is undefined for some reason
id = uuid.v4();
}
res.send({ result: "OK", message: "Session updated", id });
});
app.delete("/logout", function (request, response) {
request.session.destroy(function () {
response.send({ result: "OK", message: "Session destroyed" });
});
});
app.get("/subscribe", (req, res) => {
var channel = req.query.channel;
var id = req.query.id;
if (id) {
subscribe(id, channel);
res.send("OK");
} else {
res.status(404).send("User does not have an id!");
}
});
wss.on("connection", (ws, req) => {
ws.isAlive = true;
var id = req.url.split("=")[1];
ws.id = id;
console.log(`on connection.. id=${ws.id}`);
if (ws.id == undefined) {
ws.close(1011, "User is not assigned an id.");
}
sockets[ws.id] = ws;
ws.on("message", (message) => {
var channel = users[ws.id];
if (!channel) return;
if (message == "pong") {
ws.isAlive = true;
} else {
console.log("message from: " + ws.id);
broadcastEvent(ws.id, channel, message);
}
});
ws.on("close", (code, reason) => {
if (code == 1011) {
console.log(`${code}: rejecting user connecting. reason=${reason}`);
} else {
console.log(`lost connection to client=${ws.id}; code=${code}`);
terminateConnection(ws);
}
});
});
setInterval(() => {
wss.clients.forEach((ws) => {
if (!ws.isAlive && ws.id) {
return terminateConnection(ws);
}
ws.isAlive = false;
ws.send("ping");
});
}, 10000);
function terminateConnection(socket) {
delete sockets[socket.id];
if (channels[users[socket.id]]) {
channels[users[socket.id]].delete(socket.id);
}
if (socket.id in users) {
delete users[socket.id];
}
return socket.terminate();
}
function broadcastEvent(sender, channel, payload) {
var users = channels[channel];
users.forEach((usr) => {
if (usr != sender) {
if (sockets[usr]) {
sockets[usr].send(payload);
}
}
});
}
function subscribe(user, channel) {
console.log("subscribing user=" + user + " to channel=" + channel);
if (!(channel in channels)) {
channels[channel] = new Set();
}
var oldChannel = users[user];
if (user in users && oldChannel != channel) {
channels[oldChannel].delete(user);
}
users[user] = channel;
channels[channel].add(user);
}
server.listen(port, hostname);
console.log(`Server running at ${hostname}:${port}`);