-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
52 lines (47 loc) · 1.57 KB
/
server.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
/*
* pubsub-scribble server
*
* @license MIT License
* @author Thomas Heidrich, Adrian Kummerländer
* @copyright Copyright (c) 2012 Thomas Heidrich and other authors
*/
var io = require('socket.io').listen(8080);
var check = require('ender-validator').check;
io.configure(function(){
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging
io.set('heartbeat interval', 6); // short period heartbeats
io.set('heartbeat timeout', 10); // and timeout
// enable all supported transports
io.set('transports', [
// 'xhr-polling',
'websocket',
// 'jsonp-polling',
]);
});
// checks for malicious content
// { x: 204, y: 380, c: 'FF0000', oldX: 203, oldY: 379, t: '0' }
var isValid = function(posObj){
var retval = true;
try{
check(posObj.x).isInt().min(0).max(10000);
check(posObj.y).isInt().min(0).max(10000);
check(posObj.oldX).isInt().min(0).max(10000);
check(posObj.oldY).isInt().min(0).max(10000);
check(posObj.t).isInt().min(0).max(2);
check(posObj.c).regex(/^[0-9a-f]{6}$/i);
}catch(e){
retval = false;
}
return retval;
};
io.sockets.on('connection', function(socket) {
socket.on('move', function(pos){
if(isValid(pos)){
io.sockets.emit('moved', pos);
}
});
});
module.exports.isValid = isValid;