Browse Source

Issue #2762. Add callback to close function.

v0.9.1-release
Mikeal Rogers 13 years ago
committed by isaacs
parent
commit
d530ee62cd
  1. 2
      doc/api/http.markdown
  2. 4
      doc/api/net.markdown
  3. 5
      lib/net.js
  4. 8
      test/simple/test-net-server-close.js

2
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).

4
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()

5
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();

8
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();
});
@ -36,7 +38,3 @@ var server = net.createServer(function(socket) {
server.listen(common.PORT, function() {
net.createConnection(common.PORT);
});
server.on('close', function() {
assert.equal(server.connections, 0);
});

Loading…
Cancel
Save