-
Notifications
You must be signed in to change notification settings - Fork 0
/
wss.js
99 lines (79 loc) · 2.07 KB
/
wss.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
var config = require('./config.json');
var WebSocketServer = require('ws').Server;
var influx = require('./db'),
util = require('util');
/**
* Create WebSocket server
*/
exports.create = function() {
var wss = new WebSocketServer({
port : config.wss.port,
verifyClient : verifyClient,
clientTracking : false // disable tracking all clients, disable broadcasting
});
wss.on('connection', function(ws) {
var msg = {
'code' : 200,
'type' : 'WELCOME',
'message' : 'Welcome to Cerberus'
};
ws.send(JSON.stringify(msg));
ws.on('message', function(metrics) {
try {
metrics = JSON.parse(metrics);
} catch(ex) {
return;
}
console.log('received: %j', metrics);
var timestamp = new Date().toISOString();
// send metrics to Database
influx.write({
"database": "localhost",
"points": [
{
"name": "latency",
"tags": {
"host": metrics._meta.host,
"path": metrics.req.path
},
"timestamp": timestamp,
"fields": {
"ms": parseFloat(metrics.res.delay.ms),
"ns": metrics.res.delay.ns
}
},
{
"name": "payload",
"tags": {
"host": metrics._meta.host,
"path": metrics.req.path
},
"timestamp": timestamp,
"fields": {
"size": parseInt(metrics.res.contentLength)
}
}
]
});
});
});
};
/**
* This function will verify the client to allow publishing analytics to
* our websocket server
*
* TODO: add security policies
*/
function verifyClient(info, callback) {
var verified = false;
var criteria = {
secure : info.secure,
origin : info.origin,
headers : info.req.headers
};
// needs to secure to be set to 'true'
// check origin whitelist
// check additional security headers
verified = true;
callback(verified);
}