-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (51 loc) · 1.84 KB
/
Copy pathserver.js
File metadata and controls
59 lines (51 loc) · 1.84 KB
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
var express = require('express');
var sockjs = require('sockjs');
var http = require('http');
var redis = require('redis');
var url = require('url');
var jwt = require('jsonwebtoken');
var nconf = require('nconf');
var sockjs_opts = {sockjs_url: "http://cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js"};
var sockjs_chat = sockjs.createServer(sockjs_opts);
// load configuration
nconf.argv().env().file({ file: './config.json' });
sockjs_chat.on('connection', function(conn) {
var params = url.parse(conn.url, true).query;
jwt.verify(params.token, nconf.get('jwt:private_key'), function(err, decoded) {
if (err) {
conn.write("Failed to authenticate token.");
conn.close();
}
else{
//var user_id = params.user_id;
var user_id = decoded.user_id;
if(user_id) {
var channel_key = ["socket", user_id].join('_');
var browser = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
browser.subscribe(channel_key);
// When we see a message on the user's channel, send it to the browser
browser.on("message", function (channel, message) {
if(message != 1) { //A 1 is sent to see if the user is online
conn.write(message);
}
});
conn.on('close', function () {
console.log("Connection closed");
browser.quit(); //Get rid of the redis connection
});
}
else{
conn.write("Missing user id.");
conn.close();
}
}
});
});
// Express server
var app = express();
var server = http.createServer(app);
sockjs_chat.installHandlers(server, {prefix:'/chat'});
var port = process.env.NODE_PORT || 9001
server.listen(port, '0.0.0.0');
console.log(" Listening on 0.0.0.0:" + port);
console.log(" Using redis at " + nconf.get("redis:host") + ":" + nconf.get("redis:port"))