diff --git a/doc/api/http.markdown b/doc/api/http.markdown index 70be739b2e1..6c4dbf2b36b 100644 --- a/doc/api/http.markdown +++ b/doc/api/http.markdown @@ -137,7 +137,7 @@ a listener for the ['listening'](net.html#event_listening_) event. See also [net.Server.listen()](net.html#server.listen). -### server.close() +### server.close([cb]) Stops the server from accepting new connections. See [net.Server.close()](net.html#server.close). diff --git a/doc/api/net.markdown b/doc/api/net.markdown index 28a0601e526..23c67a2f290 100644 --- a/doc/api/net.markdown +++ b/doc/api/net.markdown @@ -162,11 +162,11 @@ Stop accepting connections for the given number of milliseconds (default is one second). This could be useful for throttling new connections against DoS attacks or other oversubscription. -#### server.close() +#### server.close([cb]) Stops the server from accepting new connections. This function is asynchronous, the server is finally closed when the server emits a `'close'` -event. +event. Optionally, you can pass a callback to listen for the `'close'` event. #### server.address() diff --git a/lib/net.js b/lib/net.js index 9d51855363a..b89cd23820f 100644 --- a/lib/net.js +++ b/lib/net.js @@ -885,12 +885,15 @@ function onconnection(clientHandle) { } -Server.prototype.close = function() { +Server.prototype.close = function(cb) { if (!this._handle) { // Throw error. Follows net_legacy behaviour. throw new Error('Not running'); } + if (cb) { + this.once('close', cb); + } this._handle.close(); this._handle = null; this._emitCloseIfDrained(); diff --git a/test/simple/test-net-server-close.js b/test/simple/test-net-server-close.js index 916d8ef1131..c3036979f78 100644 --- a/test/simple/test-net-server-close.js +++ b/test/simple/test-net-server-close.js @@ -27,7 +27,9 @@ var assert = require('assert'); var net = require('net'); var server = net.createServer(function(socket) { - server.close(); + server.close(function() { + assert.equal(server.connections, 0); + }); process.nextTick(function() { socket.destroy(); }); @@ -35,8 +37,4 @@ var server = net.createServer(function(socket) { server.listen(common.PORT, function() { net.createConnection(common.PORT); -}); - -server.on('close', function() { - assert.equal(server.connections, 0); -}); +}); \ No newline at end of file