-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
64 lines (57 loc) · 1.42 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
'use strict';
var config = require("./config");
var WebSocketServer = require("ws").Server;
var wss = new WebSocketServer({ port: config.port });
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function each(client) {
client.send(data);
});
};
console.log("Listening for connections", { port: config.port });
wss.on("connection", function (ws) {
var sendMessage = function(data, client) {
client = client || ws;
client.send(JSON.stringify(data));
};
var sendError = function(err, client) {
client = client || ws;
client.send(JSON.stringify({ error: err }));
};
console.log("Websocket connection");
ws.rooms = [];
ws.on("message", function (msg) {
try {
msg = JSON.parse(msg);
} catch (e) {
console.error({ cause: "JSON.parse(msg)", msg: msg, error: e });
return;
}
console.log(msg);
if (msg.type == "subscribe") {
if (!msg.room) {
sendError("Missing parameters");
}
if (Array.isArray(msg.room)) {
msg.room.forEach(function(room) {
ws.rooms.push(room);
});
} else {
ws.rooms.push(msg.room);
}
sendMessage({ evt: "Subscribed", room: ws.rooms });
}
if (msg.type == "broadcast") {
wss.clients.forEach(function(client) {
if (!msg.room) {
sendError("Missing parameters");
}
if (client.rooms.indexOf(msg.room) != -1) {
// console.log(client);
sendMessage(msg, client);
}
});
}
});
ws.on("close", function () {
});
});