From e1fbb967685f400e4a4bceddb4eb0aeaa0723400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Marteaux?= Date: Wed, 28 Oct 2015 22:48:08 +0100 Subject: [PATCH] Can provide ip to bind on client mode (only udp) --- lib/client.js | 6 ++++++ lib/utils.js | 8 ++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/client.js b/lib/client.js index bf204d4..def8f9b 100644 --- a/lib/client.js +++ b/lib/client.js @@ -61,6 +61,12 @@ var Request = exports.Request = function(opts) { if (!this.server || !this.server.address || !net.isIP(this.server.address)) throw new Error('Server object must be supplied with at least address'); + if (this.server.bind && !net.isIP(this.server.bind)) + throw new Error('Server object bind attribute must be an IP address'); + + if (this.server.bind && this.server.type == 'tcp') + throw new Error('Bind attribute is not supported on tcp mode, sorry'); + if (!this.server.type || ['udp', 'tcp'].indexOf(this.server.type) === -1) this.server.type = 'udp'; diff --git a/lib/utils.js b/lib/utils.js index 47bf497..80ed8b7 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -24,7 +24,8 @@ var dgram = require('dgram'), net = require('net'), util = require('util'); -var UDPSocket = exports.UDPSocket = function(socket, remote) { +var UDPSocket = exports.UDPSocket = function(socket, remote, bind) { + this._bind = bind; this._socket = socket; this._remote = remote; this._buff = undefined; @@ -72,7 +73,10 @@ UDPSocket.prototype.bind = function(type) { self.emit('close'); }); - this._socket.bind(); + if (this._bind) + this._socket.bind(null, this._bind); + else + this._socket.bind(); } };