ws icon indicating copy to clipboard operation
ws copied to clipboard

No way to detect half-closed websockets

Open quakemmo opened this issue 9 months ago • 3 comments

Is there an existing issue for this?

  • [X] I've searched for any related issues and avoided creating a duplicate issue.

Description

Is there any way to detect when the remote websocket client has half-closed the connection?

I would like to drop the connection upon this happening but I don't seem to find a way to detect this.

Thank you.

ws version

1.0.34

Node.js Version

v18.13.0

System

Linux Debian 12

Expected result

Some kind of on('end') handler.

Actual result

No on('end') (or similar) handler to be found in the docs or issues.

Attachments

No response

quakemmo avatar May 09 '24 17:05 quakemmo

No, there isn't. You can use the 'conclude' event of the Receiver but that is an internal only event. It is emitted when a peer receives a close frame.

websocket._receiver.on('conclude', handler);

You shouldn't close the connection when this event is emitted because the closing handshake is not yet complete. The connection should be closed only after the close frame has been sent and received.

lpinca avatar May 09 '24 18:05 lpinca

Thank you for your reply.

Would it be correct to use the 'conclude' event to set a timer and if, say, 10 seconds later the connection is still not closed, terminate() it?

quakemmo avatar May 10 '24 07:05 quakemmo

The library forcibly closes the connection if it is not closed properly 30 seconds after calling websocket.close() (which is called when the 'conclude' event is emitted). See also https://github.com/websockets/ws?tab=readme-ov-file#how-to-detect-and-close-broken-connections.

lpinca avatar May 10 '24 07:05 lpinca

I'm closing this as answered.

lpinca avatar May 14 '24 13:05 lpinca