Skip to content

Instantly share code, notes, and snippets.

@dshaw
Created November 30, 2011 21:56
Show Gist options
  • Save dshaw/1411155 to your computer and use it in GitHub Desktop.
Save dshaw/1411155 to your computer and use it in GitHub Desktop.
socket.io-client node fail
/**
* Module dependencies
*/
var io = require('socket.io-client');
/**
* Configuration.
*/
var url = 'http://localhost:8124'
, clientId = process.argv[2] || 0
, payloads = 0;
/**
* Socket.io Client
*/
console.log('clientId', clientId, 'socket server', url)
var socket = io.connect(url); //, { transports: ['xhr-polling']}
socket.on('connecting', function (transport) {
console.log('clientId', clientId, 'connecting', transport)
});
socket.on('connect', function () {
console.log('clientId', clientId, 'connected')
});
socket.on('message', function () {
payloads++;
console.log('clientId', clientId, 'payloads', payloads)
});
['connect_failed', 'close', 'reconnect_failed', 'disconnect'].forEach(function(event) {
socket.on(event, function () {
console.log('clientId', clientId, event, arguments)
});
})
{
"name": "sioc-node-debug"
, "version": "0.0.0"
, "description": "Socket.io-client node test"
, "author": "Daniel D. Shaw <dshaw@dshaw.com> (http://dshaw.com)"
, "dependencies": {
"socket.io": "0.8.7"
, "socket.io-client": "https://github.com/dshaw/socket.io-client/tarball/patch/ws"
}
, "engines": { "node": "*" }
}
var http = require('http')
, sio = require('socket.io')
, port = process.argv[2] || 8124
, server = http.createServer(function(req, res){
console.log('Hello.io http request');
res.writeHead(200, {'Content-Type': 'text/html'});
var body = ["<!DOCTYPE html>\n"
, "<script src='/socket.io/socket.io.js'></script>\n"
, "<script>\n"
, "window.onload = function () {\n"
, " var socket = window.socket = io.connect(),\n"
, " hello = document.getElementById('hello'),\n"
, " log = function (msg) { console.log(msg); hello.innerHTML += msg;};\n"
, " hello.onkeyup = function (e) {\n"
, " var char = String.fromCharCode(e.keyCode)[(e.shiftKey) ? 'toLocaleUpperCase' : 'toLocaleLowerCase']();\n"
, " console.log('sending: ', char);\n"
, " socket.send(char);\n"
, " };\n"
, " socket.on('connecting', function (transport) { log(' connecting ' + transport + ' '); });\n"
, " socket.on('message', function (msg) { console.log('receiving: ', msg); log(' '+msg+' '); });\n"
, " socket.on('name', function (msg) { console.log(msg); });\n"
, " ['connect','connect_failed','disconnect'].forEach(function(event) {\n"
, " socket.on(event, function () { log(' '+event+' '\n); });\n"
, " });\n"
, "};\n"
, "</script>\n"
, "<body id=hello contentEditable autofocus>Open your console...</body>\n\n"].join('');
res.end(body);
});
server.listen(port);
server.on('listening', function () { console.log('Hello.io listening on :%d', port) });
var io = sio.listen(server)
io.set('transports', ['websocket'])
io.sockets.on('connection', function (socket) {
socket.send('Hello, socket.io-client');
setTimeout(function () {
socket.send('ping');
}, 500)
socket.on('message', function (msg) { console.log(socket.id, 'echo', msg) })
socket.on('disconnect', function () { console.log(socket.id, 'disconnected') })
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment