io.sockets.on('connection', function (socket) {
var someState = { ... };
socket.on('this', function (...) { ... });
socket.on('that', function (...) { ... });
socket.on('the other thing', function (...) { ... });
});
Didn't scale very well. Organization became difficult, and much spaghetti code ensued.
var io = require('socket.io').listen(80);
var socketron = require('socketron');
var router = socketron.init(io);
router
.state({
name: 'lobby',
default: true,
on: {
'start:game': function (message, state, socket) {
state.moveAllTo(state.parent().getSubstate('game'));
}
}
})
.state('game', {
'$add': function (state, socket) {
state.players[socket.id] = { x: 0, y: 0};
},
'move:player': function (message, state, socket) {
state.players[socket.id].x = message.x;
state.players[socket.id].y = message.y;
state.broadcast('update:player', state.players[socket.id]);
}
});