Created
October 4, 2011 06:46
-
-
Save DamonOehlman/1261033 to your computer and use it in GitHub Desktop.
Bridging eve -> socket.io -> eve
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
var socket = io.connect(server), | |
reSock = /^sock\:.*$/i; | |
socket.on('connect', function() { | |
eve.on('*', function() { | |
// get the last argument | |
var lastArg = arguments.length ? arguments[arguments.length - 1] : null; | |
// if the last arg is a socket connection, then don't send it to the serve | |
// because we got it from the server... | |
if ((! lastArg) || (! reSock.test(lastArg))) { | |
socket.emit.apply(socket, ['event', eve.nt()].concat(Array.prototype.slice.call(arguments, 0))); | |
} // if | |
}); | |
socket.on('event', function(evtName) { | |
eve.apply(eve, [evtName, DECKEM].concat(Array.prototype.slice.call(arguments, 1))); | |
}); | |
}); |
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
io.sockets.on('connection', function(socket) { | |
socket.on('event', function() { | |
var emitArgs = ['event'].concat(Array.prototype.slice.call(arguments, 0)).concat('sock:' + socket.id); | |
// send the event to other connected listeners | |
// and add the client id to the end of the list so it can be distinguished on the client | |
socket.broadcast.emit.apply(socket, emitArgs); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code has been ripped from two projects I'm working on: