Created
April 22, 2012 18:08
-
-
Save oleics/2465807 to your computer and use it in GitHub Desktop.
UNIX domain sockets
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 net = require('net'); | |
connect(); | |
function connect() { | |
var client = net.connect('/tmp/test.sock', function() { //'connect' listener | |
console.log('client connected'); | |
client.write('world!'); | |
function ping() { | |
if(client.writable) { | |
client.write('ping'); | |
setTimeout(ping, 1000); | |
} | |
} | |
ping(); | |
}); | |
client.on('error', function(err) { | |
if(err.code == 'ECONNREFUSED') { | |
return setTimeout(connect, 1000); | |
} | |
throw err | |
}); | |
client.on('data', function(data) { | |
console.log(data.toString()); | |
}); | |
client.on('end', function() { | |
console.log('client disconnected'); | |
connect(); | |
}); | |
} |
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 net = require('net') | |
, fs = require('fs') | |
; | |
var server = net.createServer(function(c) { //'connection' listener | |
console.log('server connected'); | |
c.on('end', function() { | |
console.log('server disconnected'); | |
}); | |
c.write('hello'); | |
c.pipe(c); | |
}); | |
server.listen('/tmp/test.sock', function() { //'listening' listener | |
fs.chmod('/tmp/test.sock', 0700, function(err) { | |
if(err) throw err; | |
console.log('server bound'); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment