Skip to content

Instantly share code, notes, and snippets.

@Marak
Created May 15, 2012 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Marak/2698366 to your computer and use it in GitHub Desktop.
Save Marak/2698366 to your computer and use it in GitHub Desktop.
connecting to a dnode socket.io server from node
var io = require('socket.io-client');
var sock = io.connect('http://localhost:8080');
var dnode = require('dnode');
var Stream = require('stream');
var stream = new Stream;
stream.writable = true;
stream.readable = true;
stream.write = function (buf) {
sock.emit('message', String(buf));
};
stream.destroy = stream.end = function () {
sock.disconnect();
stream.emit('end');
};
sock.on('message', function (msg) {
stream.emit('data', msg);
});
sock.on('connect', function () {
stream.emit('connect');
});
dnode.connect(stream, function (remote) {
remote.cat(function (says) {
console.log('the cat says: ' + says);
});
});
$ node client.js
the cat says: meow
$
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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment