net: return this from setNoDelay and setKeepAlive
Modifies the Socket.setNoDelay and Socket.setKeepAlive methods to return the socket instance instead of undefined, to allow for chaining. PR-URL: https://github.com/nodejs/io.js/pull/1779 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
39dde3222e
commit
cb381fe3e0
@ -464,6 +464,8 @@ algorithm, they buffer data before sending it off. Setting `true` for
|
|||||||
`noDelay` will immediately fire off data each time `socket.write()` is called.
|
`noDelay` will immediately fire off data each time `socket.write()` is called.
|
||||||
`noDelay` defaults to `true`.
|
`noDelay` defaults to `true`.
|
||||||
|
|
||||||
|
Returns `socket`.
|
||||||
|
|
||||||
### socket.setKeepAlive([enable][, initialDelay])
|
### socket.setKeepAlive([enable][, initialDelay])
|
||||||
|
|
||||||
Enable/disable keep-alive functionality, and optionally set the initial
|
Enable/disable keep-alive functionality, and optionally set the initial
|
||||||
@ -475,6 +477,8 @@ data packet received and the first keepalive probe. Setting 0 for
|
|||||||
initialDelay will leave the value unchanged from the default
|
initialDelay will leave the value unchanged from the default
|
||||||
(or previous) setting. Defaults to `0`.
|
(or previous) setting. Defaults to `0`.
|
||||||
|
|
||||||
|
Returns `socket`.
|
||||||
|
|
||||||
### socket.address()
|
### socket.address()
|
||||||
|
|
||||||
Returns the bound address, the address family name and port of the
|
Returns the bound address, the address family name and port of the
|
||||||
|
@ -324,23 +324,27 @@ Socket.prototype.setNoDelay = function(enable) {
|
|||||||
if (!this._handle) {
|
if (!this._handle) {
|
||||||
this.once('connect',
|
this.once('connect',
|
||||||
enable ? this.setNoDelay : this.setNoDelay.bind(this, enable));
|
enable ? this.setNoDelay : this.setNoDelay.bind(this, enable));
|
||||||
return;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// backwards compatibility: assume true when `enable` is omitted
|
// backwards compatibility: assume true when `enable` is omitted
|
||||||
if (this._handle.setNoDelay)
|
if (this._handle.setNoDelay)
|
||||||
this._handle.setNoDelay(enable === undefined ? true : !!enable);
|
this._handle.setNoDelay(enable === undefined ? true : !!enable);
|
||||||
|
|
||||||
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Socket.prototype.setKeepAlive = function(setting, msecs) {
|
Socket.prototype.setKeepAlive = function(setting, msecs) {
|
||||||
if (!this._handle) {
|
if (!this._handle) {
|
||||||
this.once('connect', this.setKeepAlive.bind(this, setting, msecs));
|
this.once('connect', this.setKeepAlive.bind(this, setting, msecs));
|
||||||
return;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this._handle.setKeepAlive)
|
if (this._handle.setKeepAlive)
|
||||||
this._handle.setKeepAlive(setting, ~~(msecs / 1000));
|
this._handle.setKeepAlive(setting, ~~(msecs / 1000));
|
||||||
|
|
||||||
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,7 +18,8 @@ echoServer.on('listening', function() {
|
|||||||
var clientConnection = new net.Socket();
|
var clientConnection = new net.Socket();
|
||||||
// send a keepalive packet after 1000 ms
|
// send a keepalive packet after 1000 ms
|
||||||
// and make sure it persists
|
// and make sure it persists
|
||||||
clientConnection.setKeepAlive(true, 400);
|
var s = clientConnection.setKeepAlive(true, 400);
|
||||||
|
assert.ok(s instanceof net.Socket);
|
||||||
clientConnection.connect(common.PORT);
|
clientConnection.connect(common.PORT);
|
||||||
clientConnection.setTimeout(0);
|
clientConnection.setTimeout(0);
|
||||||
|
|
||||||
|
@ -24,7 +24,8 @@ echoServer.on('listening', function() {
|
|||||||
// setNoDelay before the handle is created
|
// setNoDelay before the handle is created
|
||||||
// there is probably a better way to test this
|
// there is probably a better way to test this
|
||||||
|
|
||||||
sock1.setNoDelay();
|
var s = sock1.setNoDelay();
|
||||||
|
assert.ok(s instanceof net.Socket);
|
||||||
sock1.connect(common.PORT);
|
sock1.connect(common.PORT);
|
||||||
sock1.on('end', function() {
|
sock1.on('end', function() {
|
||||||
assert.equal(callCount, 1);
|
assert.equal(callCount, 1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user