Created
June 21, 2012 07:40
-
-
Save guybrush/2964418 to your computer and use it in GitHub Desktop.
dnode reconnect
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
var dnode = require('dnode') | |
var net = require('net') | |
var port = 3000 | |
connect() | |
setTimeout(listen,1000) | |
function connect() { | |
console.log('connecting') | |
var netClient = net.connect(port) | |
var dnodeClient = dnode({name:function(cb){cb('client')}}) | |
dnodeClient.on('remote',function(r,c){ | |
console.log('dnodeClient got remote') | |
console.log('whats the servers name?') | |
r.name(function(name){console.log('the server-name is: '+name)}) | |
}) | |
netClient.pipe(dnodeClient).pipe(netClient) | |
netClient.on('error',reconnect) | |
netClient.on('end',reconnect) | |
} | |
function reconnect() { | |
console.log('going to reconnect') | |
setTimeout(connect,2000) | |
} | |
function listen(cb) { | |
console.log('starting server') | |
var server = net.createServer(function(c){ | |
var dnodeServer = dnode({name:function(cb){cb('server')}}) | |
dnodeServer.on('remote',function(r,c){ | |
console.log('dnodeServer got remote') | |
console.log('whats the remote client name?') | |
r.name(function(name){console.log('the client-name is: '+name)}) | |
setTimeout(function(){ | |
console.log('will drop the client now') | |
c.end() | |
},1000) | |
}) | |
c.pipe(dnodeServer).pipe(c) | |
}).listen(port,cb) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment