Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Trying to get a socket.io example working with a node client. They're not talking. :(
var dnode = require('dnode');
dnode.connect(5000, function (remote) {
remote.cat(function (says) {
console.log(says);
});
});
var dnode = require('./node_modules/dnode/browser');
dnode.connect({
proto: 'http:',
host: 'localhost:8080'
}, function (remote) {
remote.cat(function (says) {
console.log(says);
});
});

Vanilla dnode

./server-vanilla.js

$ node server-vanilla.js 
dnode://localhost:50000

./client-vanilla.js

$ node client-vanilla.js 
meow

(This is expected behavior.)

Socket.IO dnode

./server.js

$ node server.js 
http://localhost:8080

./client.js

$ node client.js 

localhost:8080

The cat says meow.

(This is unexpected behavior. client.js should "meow" the same as client-vanilla.js.)

socket.io raw (dnode forks)

./socket.io-server.js

$ node socket.io-server.js 
   info  - socket.io started
   debug - client authorized
   info  - handshake authorized 8987202171280180374
   debug - setting request GET /socket.io/1/websocket/8987202171280180374
   debug - set heartbeat interval for client 8987202171280180374
   debug - client authorized for 
   debug - websocket writing 1::
   debug - set close timeout for client 8987202171280180374
   info  - socket error Error: write EPIPE
    at errnoException (net.js:670:11)
    at Object.afterWrite [as oncomplete] (net.js:503:19)
   debug - setting request GET /socket.io/1/xhr-polling/8987202171280180374?t=1335403678728
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client 8987202171280180374
   debug - cleared heartbeat interval for client 8987202171280180374
   debug - clearing poll timeout
   info  - transport end
   debug - set close timeout for client 8987202171280180374
   debug - cleared close timeout for client 8987202171280180374
   debug - discarding transport

./socket.io-client.js

$ node socket.io-client.js 
The "sys" module is now called "util". It should have a similar interface.
var dnode = require('dnode');
dnode(function (client) {
this.cat = function (cb) {
cb('meow');
};
}).listen(5000);
console.log('dnode://localhost:50000');
var http = require('http');
var fs = require('fs');
var dnode = require('dnode');
var index = fs.readFileSync(__dirname + '/index.html');
var server = http.createServer(function (req, res) {
if (req.url === '/') {
res.writeHead(200, { 'Content-Type' : 'text/html' });
res.end(index);
}
});
dnode(function (client) {
this.cat = function (cb) {
cb('meow');
};
}).listen(server);
server.listen(8080);
console.log('http://localhost:8080');
#!/usr/bin/env node
var io = require('socket.io-client'),
sock = io.connect('http://localhost:8080');
sock.on('pong', function (msg) {
console.log(msg);
});
sock.on('connect', function () {
console.log('connected');
setInterval(function () {
console.log('ping.');
sock.emit('ping', 'ping.');
}, 1000);
});
sock.on('ready', function () { console.log('ready'); });
var server = require('http').createServer(),
io = require('socket.io').listen(server);
io.sockets.on('connection', function (sock) {
sock.on('ping', function (data) {
console.log('pong.');
sock.emit('pong', 'pong.');
});
});
server.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.