Created
July 17, 2013 03:13
-
-
Save andrejewski/6017392 to your computer and use it in GitHub Desktop.
Socket.io router extension. A little customization to help with combining Express/Connect and Socket.io. In both CoffeeScript and JavaScript, for your preference.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# socket.coffee | |
module.exports = (soc) -> | |
debug = true | |
# router | |
soc.routes = [] | |
soc.mount = (action, next = ->) -> | |
soc.routes.push [action, next] | |
soc.router = -> | |
soc.sockets.on 'connection', (socket) -> | |
soc.routes.forEach (route) -> | |
[action, next] = route | |
_action = action | |
socket.on action, (data, done) -> | |
(console.time 'socket/'+_action) if debug | |
thus = (error, data, action) -> | |
(console.timeEnd 'socket/'+_action) if debug | |
done {error, data, action} | |
next.call socket, data, thus, soc.sockets | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by CoffeeScript 1.6.3 | |
(function() { | |
module.exports = function(soc) { | |
var debug; | |
debug = true; | |
soc.routes = []; | |
soc.mount = function(action, next) { | |
if (next == null) { | |
next = function() {}; | |
} | |
return soc.routes.push([action, next]); | |
}; | |
return soc.router = function() { | |
return soc.sockets.on('connection', function(socket) { | |
return soc.routes.forEach(function(route) { | |
var action, next, _action; | |
action = route[0], next = route[1]; | |
_action = action; | |
return socket.on(action, function(data, done) { | |
var thus; | |
if (debug) { | |
console.time('socket/' + _action); | |
} | |
thus = function(error, data, action) { | |
if (debug) { | |
console.timeEnd('socket/' + _action); | |
} | |
return done({ | |
error: error, | |
data: data, | |
action: action | |
}); | |
}; | |
return next.call(socket, data, thus, soc.sockets); | |
}); | |
}); | |
}); | |
}; | |
}; | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment