Created
May 15, 2012 01:06
-
-
Save Marak/2698366 to your computer and use it in GitHub Desktop.
connecting to a dnode socket.io server from node
This file contains hidden or 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
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); | |
}); | |
}); |
This file contains hidden or 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
$ node client.js | |
the cat says: meow | |
$ |
This file contains hidden or 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
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