Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server socket does not close after calling .close() #496

Closed

Conversation

michaelmcmillan
Copy link

This test shows that the socket on the WebSocketServer does not close after calling .close().

As others have suggested, the .close() method should be an async function that accepts a callback
for when it is finished with terminating the clients and closing up the socket.

ws/lib/WebSocketServer.js

Lines 101 to 131 in 71ff077

WebSocketServer.prototype.close = function() {
// terminate all associated clients
var error = null;
try {
for (var i = 0, l = this.clients.length; i < l; ++i) {
this.clients[i].terminate();
}
}
catch (e) {
error = e;
}
// remove path descriptor, if any
if (this.path && this._server._webSocketPaths) {
delete this._server._webSocketPaths[this.path];
if (Object.keys(this._server._webSocketPaths).length == 0) {
delete this._server._webSocketPaths;
}
}
// close the http server if it was internally created
try {
if (typeof this._closeServer !== 'undefined') {
this._closeServer();
}
}
finally {
delete this._server;
}
if (error) throw error;
}

Related issue: #31

… after calling .close().

As others have suggested, the .close() method should be an async function that accepts a callback
for when it is finished with terminating the clients and closing up the socket.

https://github.com/websockets/ws/blob/71ff077a6b2c6aff05022521cdcd745e76cf5446/lib/WebSocketServer.js#L101-L131
@michaelmcmillan
Copy link
Author

Any advice on how I can implement this? I'll happily code it myself.

@lpinca
Copy link
Member

lpinca commented Nov 7, 2016

Hi,
I'm not sure why but the same also happens with a plain HTTP server:

'use strict';

const http = require('http');
const net = require('net');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(3000, '0.0.0.0', () => console.log('listening on *:3000'));
server.close();

const socket = net.connect(3000, () => {
  throw new Error('connected after calling close');
});

@lpinca
Copy link
Member

lpinca commented Jan 27, 2017

If you use

server.listen(3000, '::1', () => console.log('listening on *:3000'));

it works as expected, but if you use

server.listen(3000, '127.0.0.1', () => console.log('listening on *:3000'));

it doesn't and the connection is established.

Anyway this is not a ws issue. Feel free to file a bug in the Node.js repository.

@lpinca lpinca closed this Jan 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants