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

时间:2021-01-21 22:49:22

标签: c# node.js websocket ws system.net.websockets

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

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

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

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

如何检测服务器上的连接是“CLOSING”,我可以使用 ws 库的什么功能向 C# 客户端发送确认,以确保他的状态从“CloseSent”变为“Closed” ?

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

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。

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

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