Websocket Closing Handshake with a C# client and a Node.js server

853 views Asked by At

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#.

1

There are 1 answers

0
Arty On BEST ANSWER

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.