-
Notifications
You must be signed in to change notification settings - Fork 1
/
websocket.js
34 lines (32 loc) · 1.26 KB
/
websocket.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
var WebSocketServer = require('ws').Server;
// defines websockets
module.exports = function(app){
// WebSocket for logging
var wss = new WebSocketServer({ server: app.server , path: '/logbroadcast'});
app.broadcast = function broadcast(data, loglevel, sender) {
// parse message for loglevel case insensative (-i) when loglevel isn't set
if(typeof loglevel === 'undefined' || loglevel === null){
var debug = new RegExp('DEBUG', 'i');
var error = new RegExp('ERROR', 'i');
if(data.match(debug)){
loglevel = 'DEBUG';
}
else if(data.match(error)){
loglevel = 'ERROR';
}
else{
loglevel = 'INFO';
}
}
wss.clients.forEach(function each(client) {
client.send(JSON.stringify({data: data, loglevel: loglevel, sender: sender}));
});
};
// WebSocket for gadgetron state
var wssGadgetronStatus = new WebSocketServer({ server: app.server , path: '/gadgetronstate'});
app.broadcastGadgetronStatus = function broadcastGadgetronStatus(data) {
wssGadgetronStatus.clients.forEach(function each(client) {
client.send(JSON.stringify(data));
});
};
}