-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (51 loc) · 1.42 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
const express = require('express');
module.exports = function(app, frontendPath) {
require('express-ws')(app);
app.use(frontendPath, express.static('node_modules/express-ws-event-bus/frontend'));
return endpoint => {
return new Promise(function(resolve) {
let handlers = new Map();
let connections = [];
app.ws(endpoint, function(connection) {
// Every client opens a new connection.
connections.push(connection);
connection.on('message', function(message) {
try {
let event = JSON.parse(message);
const handler = handlers.get(event.type);
if (handler) {
handler(event.data, connection);
}
} catch (e) {
console.log('This doesn\'t look like a valid JSON: ', message, e);
}
});
});
resolve({
on: function(type, callback) {
handlers.set(type, callback);
},
send: function(type, data, client) {
const event = {
type: type,
data: data
};
connections = connections.filter(conn => conn.readyState === 1);
try {
if (client) {
client.send(JSON.stringify(event));
} else {
connections.forEach(conn => conn.send(JSON.stringify(event)));
}
} catch (e) {
// Probably a connection was closed between the filter and the for each
// Ignore.
}
},
connections: function() {
return connections.filter(conn => conn.readyState === 1);
}
});
});
};
};