vapor / websocket-kit

WebSocket client library built on SwiftNIO
https://docs.vapor.codes/4.0/advanced/websockets/
MIT License
277 stars 79 forks source link

Should be able to force close the WebSocket #90

Open lmcd opened 3 years ago

lmcd commented 3 years ago

Is your feature request related to a problem? Please describe.

The close method waits for acknowledgement from the server. In some situations we already know the connection is closed (e.g. if the reachability state to the server has changed), and would rather just force a disconnected state immediately.

Describe the solution you'd like

Introduce forceClose method to WebSocket, that just terminates the channel.

lmcd commented 3 years ago

Actually, I looked a bit further into this, and you're still forced to incur a 5 second penalty between closing the channel and onClose being triggered. This is a shutdown timeout for TLS connections built into NIO.

This is really frustrating, 5 seconds is an unacceptably long time for a WebSocket to be stranded in a closing state for my use case. So for example, if you set pingInterval to 1 second, you'll actually be waiting at least 6 seconds until onClose is triggered and you can attempt a connection restart.

Perhaps an onClosing future would be useful in responding to these situations?

lmcd commented 3 years ago

On further thought, since it might difficult to provide a reliable onClosing that covers all possible closing scenarios, perhaps we should just deliver a onPingTimeout that covers just this specific use case.