Created
May 4, 2012 16:22
-
-
Save rcastillo/2595882 to your computer and use it in GitHub Desktop.
server (client + server) websocket node.js
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
// (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