Skip to content

Commit 23baaa6

Browse files
silverwindandrewdeandrade
authored andcommitted
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: nodejs/node#1779 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 45cb48c commit 23baaa6

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

doc/api/net.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ algorithm, they buffer data before sending it off. Setting `true` for
464464
`noDelay` will immediately fire off data each time `socket.write()` is called.
465465
`noDelay` defaults to `true`.
466466

467+
Returns `socket`.
468+
467469
### socket.setKeepAlive([enable][, initialDelay])
468470

469471
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
475477
initialDelay will leave the value unchanged from the default
476478
(or previous) setting. Defaults to `0`.
477479

480+
Returns `socket`.
481+
478482
### socket.address()
479483

480484
Returns the bound address, the address family name and port of the

lib/net.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -324,23 +324,27 @@ Socket.prototype.setNoDelay = function(enable) {
324324
if (!this._handle) {
325325
this.once('connect',
326326
enable ? this.setNoDelay : this.setNoDelay.bind(this, enable));
327-
return;
327+
return this;
328328
}
329329

330330
// backwards compatibility: assume true when `enable` is omitted
331331
if (this._handle.setNoDelay)
332332
this._handle.setNoDelay(enable === undefined ? true : !!enable);
333+
334+
return this;
333335
};
334336

335337

336338
Socket.prototype.setKeepAlive = function(setting, msecs) {
337339
if (!this._handle) {
338340
this.once('connect', this.setKeepAlive.bind(this, setting, msecs));
339-
return;
341+
return this;
340342
}
341343

342344
if (this._handle.setKeepAlive)
343345
this._handle.setKeepAlive(setting, ~~(msecs / 1000));
346+
347+
return this;
344348
};
345349

346350

test/parallel/test-net-persistent-keepalive.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ echoServer.on('listening', function() {
1818
var clientConnection = new net.Socket();
1919
// send a keepalive packet after 1000 ms
2020
// and make sure it persists
21-
clientConnection.setKeepAlive(true, 400);
21+
var s = clientConnection.setKeepAlive(true, 400);
22+
assert.ok(s instanceof net.Socket);
2223
clientConnection.connect(common.PORT);
2324
clientConnection.setTimeout(0);
2425

test/parallel/test-net-persistent-nodelay.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ echoServer.on('listening', function() {
2424
// setNoDelay before the handle is created
2525
// there is probably a better way to test this
2626

27-
sock1.setNoDelay();
27+
var s = sock1.setNoDelay();
28+
assert.ok(s instanceof net.Socket);
2829
sock1.connect(common.PORT);
2930
sock1.on('end', function() {
3031
assert.equal(callCount, 1);

0 commit comments

Comments
 (0)