Skip to content

Instantly share code, notes, and snippets.

@rcastillo
Created May 4, 2012 16:22
Show Gist options
  • Save rcastillo/2595882 to your computer and use it in GitHub Desktop.
Save rcastillo/2595882 to your computer and use it in GitHub Desktop.
server (client + server) websocket node.js
// (server) server-side
var io = require('socket.io').listen(8000);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('ping', function (data) {
console.log(data);
});
});
// (server) client-side
var protocol = 'ws';
var domain = 'localhost';
var port = 8000;
var options = {
'max reconnection attempts' : 10,
'reconnection delay' : 2000,
'transports' : [
'websocket'
]
}
var SocketIOClient = require('socket.io-client');
var IOClient = SocketIOClient.connect(protocol + '://' + domain + ':' + port, options);
IOClient.on('connect', function(){
console.log("Connected", arguments);
IOClient.emit('ping', 'pong');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment