Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions lib/sockets/sock.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,13 @@ Socket.prototype.connect = function(port, host, fn){
if ('string' == typeof port) {
port = url.parse(port);

if (port.protocol == "unix:") {
if (port.port) {
host = port.hostname || '0.0.0.0';
port = parseInt(port.port, 10);
} else {
host = fn;
fn = undefined;
port = port.pathname;
} else {
host = port.hostname || '0.0.0.0';
port = parseInt(port.port, 10);
}
} else {
host = host || '0.0.0.0';
Expand All @@ -305,7 +305,6 @@ Socket.prototype.connect = function(port, host, fn){
var sock = new net.Socket;
sock.setNoDelay();
this.type = 'client';
port = port;

this.handleErrors(sock);

Expand Down Expand Up @@ -385,14 +384,12 @@ Socket.prototype.bind = function(port, host, fn){
if ('string' == typeof port) {
port = url.parse(port);

if ('unix:' == port.protocol) {
host = fn;
fn = undefined;
port = port.pathname;
unixSocket = true;
} else {
if (port.port) {
host = port.hostname || '0.0.0.0';
port = parseInt(port.port, 10);
} else {
port = port.pathname;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh i forgot some // (will just push -f it away)

the whole option-parsing here is kind of weird but i feel like a small PR is better than refactoring all the things \o/

unixSocket = true;
}
} else {
host = host || '0.0.0.0';
Expand All @@ -405,7 +402,9 @@ Socket.prototype.bind = function(port, host, fn){
debug('bind %s:%s', host, port);
this.server.on('listening', this.emit.bind(this, 'bind'));

if (unixSocket) {
if (!unixSocket) {
this.server.listen(port, host, fn)
} else {
// TODO: move out
this.server.on('error', function(e) {
if (e.code == 'EADDRINUSE') {
Expand All @@ -417,23 +416,25 @@ Socket.prototype.bind = function(port, host, fn){
var clientSocket = new net.Socket();

clientSocket.on('error', function(e2) {
if (e2.code == 'ECONNREFUSED') {
// No other server listening, so we can delete stale
// socket file and reopen server socket
fs.unlink(port);
self.server.listen(port, host, fn);
if (e2.code !== 'ECONNREFUSED') {
return fn && fn(e);
}
// No other server listening, so we can delete stale
// socket file and reopen server socket
fs.unlink(port, function(){
self.server.listen(port, fn);
});
});

clientSocket.connect({path: port}, function() {
// Connection is possible, so other server is listening
// on this file socket
throw e;
});
}
});
this.server.listen(port, fn)
}

this.server.listen(port, host, fn);

return this;
};
10 changes: 7 additions & 3 deletions test/test.unixsocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ var axon = require('..')
var req = axon.socket('req')
, rep = axon.socket('rep');

var path = 'unix://' + process.cwd() + '/test.sock';
var path = process.cwd() + '/test.sock'
var bound = false;

req.bind(path);
rep.connect(path);
req.bind(path, function(){
bound = true;
});

rep.on('message', function(msg, reply){
reply('got "' + msg + '"');
});

req.send('hello', function(msg){
msg.toString().should.equal('got "hello"');
bound.should.equal(true);
req.close();
rep.close();
});
});