Skip to content

Instantly share code, notes, and snippets.

@oleics
Created April 22, 2012 18:08
Show Gist options
  • Save oleics/2465807 to your computer and use it in GitHub Desktop.
Save oleics/2465807 to your computer and use it in GitHub Desktop.
UNIX domain sockets
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();
});
}
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