0

I use "CloseOutputAsync" on the C# client to initiate the close handshake with the Node.js server.

My Node.js server receives a "close" event and immediately sets his "websocket.readyState" as "CLOSED" without going through "CLOSING".

As my C# client is sending the close handshake, he sets his state as "CloseSent".

The problem is that my C# stays in this state, while my Node.js server believes that the websocket connection is closed.

How can I detect that the connection is "CLOSING" on the server and what function of the ws library can I use to send an acknoledgment to the C# client to make sure that his state goes from "CloseSent" to "Closed" ?

I am using the standard "ws" library under Node.js and the standard "System.Net.WebSockets" under C#.

Takah
  • 345
  • 3
  • 20
  • The `ws` package's current version [appears to handle this](https://github.com/websockets/ws/blob/d1a8af4ddb1b24a4ee23acf66decb0ed0e0d8862/lib/websocket.js#L227-L242). Are you using the latest version? You could check with [Wireshark](https://www.wireshark.org/) that the close packets are sent. Start it with administrative privileges, then choose the loopback traffic capture adapter (assuming you're running both on localhost), and type in `websocket` as the filter. You should see two "WebSocket Connection Close" packets: to and from. – cbr Jan 21 '21 at 23:20
  • Also, just to be clear, you do `await` the `CloseOutputAsync` call, right? Was the problem that the task never finishes and it gets stuck awaiting for it? – cbr Jan 21 '21 at 23:25
  • Thanks for the idea. I will try wireshark. Yes I await the CloseOutputAsync call, there's no problem but I'd like my websocket to be closed definitely instead of being "CloseSent" – Takah Jan 22 '21 at 06:33
  • It looks like it's the normal state to change the state to closed before calling the event ".on('close'" https://github.com/websockets/ws/blob/d1a8af4ddb1b24a4ee23acf66decb0ed0e0d8862/lib/websocket.js#L187 – Takah Jan 22 '21 at 07:14
  • I believe that there's an event called "on('end'" that will fire before "on('close'" and has a "CLOSING" state. I will try later on and let you know. Thank you for sending a link to the source code. I never thought of it ! – Takah Jan 22 '21 at 07:36

1 Answers1

0

I finally found the solution.

When the C# client is initiating the close, he should send a close with "CloseAsync" instead of "CloseOutputAsync". The node.js server immediatly sets the websocket state as "Closed" without going through "Closing" state (is never found a way to trigger this "Closing" state) but then the C# client sets his state as "Closed".

If the node.js server is initiating the close, then the C# client should use "CloseOutputAsync". The C# client states goes from Open -> CloseReceived -> CloseSent -> Closed.

Takah
  • 345
  • 3
  • 20