-
Notifications
You must be signed in to change notification settings - Fork 3
/
habitIO.js
43 lines (36 loc) · 976 Bytes
/
habitIO.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
var config = require('./config'),
app = require('./app'),
crypto = require('crypto'),
http = require('http').Server(app),
io = require('socket.io')(http);
var namespaces = {};
var port = 8081;
var serverStarted = false;
function startServer() {
if (serverStarted) { return false; }
http.listen(port, function() {
console.log('Socket.io HTTP server listening on %s', port);
});
serverStarted = true;
return true;
}
function getNamespace(id) {
if (typeof id !== 'string') {
throw new Error('Must provide a string to getNamespace.' +
' Received: ' + (typeof id));
}
if (!namespaces[id]) {
namespaces[id] = io.of('/' + getNamespaceHash(id));
}
return namespaces[id];
}
function getNamespaceHash(id) {
return crypto.createHash('sha512').update(id +
config.SOCKET_SALT).digest('hex');
}
module.exports = {
io: io,
getNamespace: getNamespace,
getNamespaceHash: getNamespaceHash,
startServer: startServer
};