Created
November 27, 2012 01:31
-
-
Save fideloper/4151820 to your computer and use it in GitHub Desktop.
Engine.io and server Client.io
This file contains hidden or 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
// Install with 'npm install engine.io-client' | |
// Run in node.js as a client! (Not browser-only!) | |
/* Socket client */ | |
var eio = require('engine.io-client'); | |
var socket = new eio.Socket({ host: 'localhost', port: 8080 }); | |
socket.onopen = function () { | |
socket.onmessage = function (data) { | |
console.log( JSON.parse(data.data) ); | |
}; | |
socket.onclose = function () { | |
}; | |
}; |
This file contains hidden or 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
//Install with 'npm install engine.io' | |
/* Socket server */ | |
var engine = require('engine.io') | |
, server = engine.listen(8080) | |
// Send message to everyone but (optionally) the sending client | |
server.broadcast = function(mssg, id) { | |
for( var key in server.clients ) { | |
if(typeof id !== 'undefined') { | |
// Don't broadcast to sending client | |
if( key == id ) { | |
continue; | |
} | |
} | |
server.clients[key].send(mssg); | |
} | |
} | |
server.on('connection', function (socket) { | |
// Broadcast a to other cients | |
server.broadcast( JSON.stringify({resp:'New Client!'}), socket.id); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment