-
Notifications
You must be signed in to change notification settings - Fork 3
/
socketio.js
91 lines (76 loc) · 2.65 KB
/
socketio.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
function Socketio() {
}
// url handling
Socketio.prototype.index = function(req, res) {
console.log('Opening up the socket.io sample');
var loginName='';
if (req.session.oauth) {
loginName = req.session.user.name;
}
res.render('socketio/index', {layout: 'socketio/layout', locals:{loginName:loginName}});
};
// socket io configuration
var io = require('socket.io');
var buffer = [];
var clients = [];
var Redis = require('./redis');
var redis = new Redis();
var sanitize = require('validator').sanitize;
redis.obtainMessages(0,15,function(replies) {
replies.forEach(function (reply, i) {
buffer.push(JSON.parse(reply));
});
buffer.reverse();
});
Socketio.prototype.init = function(app) {
io = io.listen(app);
io.on('connection', function(client) {
client.send({ buffer: buffer });
client.on('message', function(message) {
if (hackFoundInMessage(message)) return;
if ('newName' in message) {
console.log("Received a new name: " + message.newName);
clients[client.sessionId] = message.newName;
client.broadcast({ announcement: clients[client.sessionId] + ' connected' });
sendClients(client);
return;
}
var msg = { chat: [clients[client.sessionId], message.message] };
buffer.push(msg);
if (buffer.length > 15) buffer.shift();
client.broadcast(msg);
io.emit('newMessage',msg);
});
client.on('disconnect', function() {
client.broadcast({ announcement: clients[client.sessionId] + ' disconnected' });
removeClient(client.sessionId);
sendClients(client)
});
});
io.on('newMessage', function(obj) {
redis.storeMessage(JSON.stringify(obj));
});
function removeClient(id) {
delete clients[id];
}
function sendClients(client) {
var curClients = [];
for (var i in clients) {
curClients[curClients.length] = clients[i];
}
client.broadcast({users: curClients});
client.send({users: curClients});
}
function hackFoundInMessage(message) {
var hackFound = false;
var jsonMessage = JSON.stringify(message);
var xssMessage = sanitize(jsonMessage).xss();
if (jsonMessage != xssMessage) {
var hack = {"hack":{"old":jsonMessage, "new":xssMessage, "date":new Date()}};
redis.writeWarning(JSON.stringify(hack));
hackFound = true; // Might want to do something with a callback
}
return hackFound;
}
};
module.exports = Socketio;