Skip to content

Commit 63005ee

Browse files
sam-githubbnoordhuis
authored andcommitted
dgram: close() should accept a callback
Like net, http, and https server.close, and socket.end(), etc. PR-URL: #217 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 7349d7f commit 63005ee

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

doc/api/dgram.markdown

+4-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ are created.
8282

8383
### Event: 'close'
8484

85-
Emitted when a socket is closed with `close()`. No new `message` events will be emitted
85+
Emitted after a socket is closed with `close()`. No new `message` events will be emitted
8686
on this socket.
8787

8888
### Event: 'error'
@@ -228,9 +228,10 @@ shown below.
228228
});
229229

230230

231-
### socket.close()
231+
### socket.close([callback])
232232

233-
Close the underlying socket and stop listening for data on it.
233+
Close the underlying socket and stop listening for data on it. If a callback is
234+
provided, it is added as a listener for the ['close'](#dgram_event_close) event.
234235

235236
### socket.address()
236237

lib/dgram.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,9 @@ function afterSend(err) {
352352
}
353353

354354

355-
Socket.prototype.close = function() {
355+
Socket.prototype.close = function(callback) {
356+
if (callback)
357+
this.on('close', callback);
356358
this._healthCheck();
357359
this._stopReceiving();
358360
this._handle.close();

test/parallel/test-dgram-close.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ buf.fill(42);
3232
var socket = dgram.createSocket('udp4');
3333
var handle = socket._handle;
3434
var closeEvents = 0;
35+
var closeCallbacks = 0;
3536
socket.send(buf, 0, buf.length, common.PORT, 'localhost');
36-
assert.strictEqual(socket.close(), socket);
37+
assert.strictEqual(socket.close(function() {
38+
++closeCallbacks;
39+
}), socket);
3740
socket.on('close', function() {
41+
assert.equal(closeCallbacks, 1);
3842
++closeEvents;
3943
});
4044
socket = null;
@@ -48,4 +52,5 @@ setImmediate(function() {
4852

4953
process.on('exit', function() {
5054
assert.equal(closeEvents, 1);
55+
assert.equal(closeCallbacks, 1);
5156
});

0 commit comments

Comments
 (0)