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

No way to detect half-closed websockets #2225

Closed
1 task done
quakemmo opened this issue May 9, 2024 · 4 comments
Closed
1 task done

No way to detect half-closed websockets #2225

quakemmo opened this issue May 9, 2024 · 4 comments

Comments

@quakemmo
Copy link

quakemmo commented May 9, 2024

Is there an existing issue for this?

  • 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

@lpinca
Copy link
Member

lpinca commented May 9, 2024

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.

@quakemmo
Copy link
Author

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?

@lpinca
Copy link
Member

lpinca commented May 10, 2024

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
Copy link
Member

lpinca commented May 14, 2024

I'm closing this as answered.

@lpinca lpinca closed this as completed May 14, 2024
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

No branches or pull requests

2 participants