Websocket 关闭与 C# 客户端和 Node.js 服务器的握手

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

我在 C# 客户端上使用“CloseOutputAsync”来启动与 Node.js 服务器的关闭握手。

我的 Node.js 服务器接收到一个“关闭”事件并立即将他的“websocket.readyState”设置为“关闭”而不通过“关闭”。

当我的 C# 客户端发送关闭握手时,他将状态设置为“CloseSent”。

问题是我的 C# 保持这种状态,而我的 Node.js 服务器认为 websocket 连接已关闭。

我如何检测到服务器上的连接正在“关闭”以及我可以使用 ws 库的什么功能向 C# 客户端发送确认以确保他的状态从“CloseSent”变为“已关闭" ?

我正在使用 Node.js 下的标准“ws”库和 C# 下的标准“System.Net.WebSockets”库。

我终于找到了解决办法。

当 C# 客户端启动关闭时,他应该使用“CloseAsync”而不是“CloseOutputAsync”发送关闭。 node.js 服务器立即将 websocket 状态设置为“关闭”而不经过“关闭”状态(从未找到触发此“关闭”状态的方法),但随后 C# 客户端将其状态设置为“关闭”。

如果 node.js 服务器正在启动关闭,则 C# 客户端应使用“CloseOutputAsync”。 C# 客户端状态从打开 -> CloseReceived -> CloseSent -> 关闭。