Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Issue #2762. Add callback to close function.

  • Loading branch information...
mikeal authored and isaacs committed Feb 16, 2012
1 parent 9633843 commit d530ee62cd1eb1fd038f4e86314c741422b4de25
Showing with 11 additions and 10 deletions.
  1. +1 −1 doc/api/http.markdown
  2. +2 −2 doc/api/net.markdown
  3. +4 −1 lib/net.js
  4. +4 −6 test/simple/test-net-server-close.js
@@ -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).
@@ -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()
@@ -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();
@@ -27,16 +27,14 @@ 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();
});
});

server.listen(common.PORT, function() {
net.createConnection(common.PORT);
});

server.on('close', function() {
assert.equal(server.connections, 0);
});
});

1 comment on commit d530ee6

@Filirom1

This comment has been minimized.

Copy link

Filirom1 commented on d530ee6 Mar 30, 2012

Thanks, this is a simple thing but very useful :)

Please sign in to comment.
You can’t perform that action at this time.