-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (34 loc) · 1010 Bytes
/
Copy pathindex.js
File metadata and controls
42 lines (34 loc) · 1010 Bytes
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
const http = require('http');
const redis = require('redis');
const sockjs = require('sockjs');
const Message = require('./message');
const server = http.createServer();
const redisClient = redis.createClient();
const echo = sockjs.createServer({ sockjs_url: 'http://cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js' });
const connections = {};
echo.on('connection', function(conn) {
conn.on('data', function(mes) {
const message = new Message(mes);
if (message.isInfoType()) {
conn.userId = message.userId;
connections[conn.userId] = conn;
}
});
conn.on('close', function() {
delete connections[conn.userId];
});
});
redisClient.on('message', function(channel, mes) {
try {
const message = new Message(mes);
const conn = connections[message.userId];
if (conn) {
conn.write(mes);
}
} catch (er) {
console.log(er);
}
});
redisClient.subscribe('recent_challenge');
echo.installHandlers(server, {prefix: '/echo'});
server.listen(5001);