2014-09-25 10:27:33 -04:00
|
|
|
#!/usr/bin/env node
|
2016-01-03 19:34:16 -05:00
|
|
|
var express = require('express'),
|
|
|
|
app = express(),
|
|
|
|
server = require('http').createServer(app),
|
|
|
|
io = require('socket.io')(server),
|
|
|
|
port = process.env.PORT || 8080;
|
2014-09-25 10:27:33 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
server.listen(port, function(){
|
|
|
|
console.log('listening on *:'+port);
|
|
|
|
});
|
2014-09-18 12:21:07 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
// Serve /public/* as /
|
2014-09-18 12:21:07 -04:00
|
|
|
app.use(express.static(__dirname + '/public'));
|
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
io.on('connection', function(socket) {
|
|
|
|
|
|
|
|
socket.on('room:join', function(req) {
|
|
|
|
if( req ) {
|
|
|
|
socket.emit('room:joined',req);
|
|
|
|
socket.join(req);
|
|
|
|
socket.broadcast.to(req).emit('message:server', {msg:'person_joined'} );
|
|
|
|
socket.current_room = req;
|
2014-09-19 13:25:16 -04:00
|
|
|
} else {
|
2016-01-03 19:34:16 -05:00
|
|
|
socket.emit('message:server', {msg:'command_failed'} );
|
2014-09-19 13:25:16 -04:00
|
|
|
}
|
2016-01-03 19:34:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('room:leave', function(req) {
|
|
|
|
if( req ) {
|
|
|
|
socket.emit('room:left');
|
|
|
|
socket.leave(req);
|
|
|
|
socket.broadcast.to(req).emit('message:server', {msg:'person_left'} );
|
|
|
|
socket.current_room = undefined;
|
|
|
|
} else {
|
|
|
|
socket.emit('message:server', {msg:'command_failed'} );
|
|
|
|
}
|
|
|
|
});
|
2014-09-22 16:11:13 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
socket.on('room:count', function (req) {
|
|
|
|
if( socket.current_room !== undefined ) {
|
|
|
|
var clientsList = io.sockets.adapter.rooms[socket.current_room];
|
|
|
|
socket.emit('message:server', {msg:'person_count', payload: Object.keys(clientsList).length } );
|
|
|
|
} else {
|
|
|
|
socket.emit('message:server', {msg:'command_failed'} );
|
|
|
|
}
|
|
|
|
});
|
2014-09-22 16:11:13 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
socket.on('message:send', function(req) {
|
2014-09-22 16:11:13 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
// Check that the user is in a room
|
|
|
|
if(req && req.room) {
|
2014-09-22 16:11:13 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
// Check that the message size is within bounds
|
|
|
|
var total_msg_size = (req.msg) ? req.msg.length : 0 + (req.nick) ? req.nick.length : 0;
|
|
|
|
if( total_msg_size <= 4096) {
|
2014-09-22 16:11:13 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
// Check that at least 100ms has passed since last message
|
|
|
|
if( socket.last_message === undefined || new Date().getTime() - socket.last_message > 100 ) {
|
2014-09-22 16:11:13 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
socket.broadcast.to(req.room).emit('message:send', { msg: req.msg, nick: req.nick} );
|
|
|
|
socket.emit('message:send', { msg: req.msg, nick: req.nick} );
|
2014-09-22 16:11:13 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
socket.last_message = new Date().getTime();
|
2014-09-22 16:11:13 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
} else {
|
2014-09-22 16:11:13 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
// Do not complain if message rate is too fast, that would only generate more traffic
|
2014-09-22 16:11:13 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
}
|
2014-09-22 16:11:13 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
} else {
|
2014-09-22 16:11:13 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
// Message size is out of bounds, complain
|
|
|
|
socket.emit('message:server', {msg:'command_failed'} );
|
|
|
|
}
|
2014-09-22 16:11:13 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
}
|
2014-09-18 12:21:07 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
});
|
2014-09-18 17:08:13 -04:00
|
|
|
|
2016-01-03 19:34:16 -05:00
|
|
|
socket.on('disconnect', function() {
|
|
|
|
// Notify other users of the room
|
|
|
|
if( socket.current_room !== undefined ) {
|
|
|
|
socket.broadcast.to(socket.current_room).emit('message:server', {msg:'person_left'} );
|
|
|
|
}
|
|
|
|
});
|
2014-10-20 13:14:44 -04:00
|
|
|
|
2014-09-25 10:27:33 -04:00
|
|
|
});
|