cryptalk/server.js

39 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-09-18 12:21:07 -04:00
var express = require('express.io'),
uuid = require('node-uuid');
app = express();app.http().io();
app.use(express.static(__dirname + '/public'));
app.io.route('room', {
2014-09-18 13:17:41 -04:00
generate: function(req) {
2014-09-18 12:21:07 -04:00
var room = uuid.v4();
2014-09-18 13:17:41 -04:00
req.socket.emit('message:server', 'Room ' + room + ' generated');
req.socket.emit('room:generated',room);
2014-09-18 12:21:07 -04:00
},
join: function(req) {
if(req.data) {
req.socket.emit('room:joined',req.data);
req.socket.join(req.data);
req.socket.broadcast.to(req.data).emit('message:server', 'A person joined this room');
}
},
leave: function(req) {
if(req.data) {
req.socket.emit('room:left');
req.socket.leave(req.data);
req.socket.broadcast.to(req.data).emit('message:server', 'A person left this room');
}
}
});
app.io.route('message', {
send: function(req) {
2014-09-18 13:17:41 -04:00
if(req.data && req.data.room) req.socket.broadcast.to(req.data.room).emit('message:send', { msg: req.data.msg, nick: req.data.nick} );
req.socket.emit('message:send', { msg: req.data.msg, nick: req.data.nick} );
2014-09-18 12:21:07 -04:00
}
});
app.listen(8080, function(){
console.log('listening on *:8080');
});