gpt4 book ai didi

websocket - 为什么客户端 websocket 关闭代码与服务器代码不匹配?

转载 作者:行者123 更新时间:2023-12-05 04:15:20 25 4
gpt4 key购买 nike

我有一个 Spring Boot Tomcat 服务器正在处理来自正在使用的客户端的 websocket 连接:

  1. 套接字火箭
  2. 泰鲁斯

我发现服务端提供的关闭码往往不是客户端读取的关闭码。

对于SocketRocket,我在服务端用code 1000关闭websocket,客户端经常读取1001。

对于 Tyrus,我使用代码 1011 关闭 websocket,客户端读取 1006 或 1011。

来自 RFC 6455 的关闭代码说明:

1000 indicates a normal closure, meaning that the purpose forwhich the connection was established has been fulfilled.

1001 indicates that an endpoint is "going away", such as a servergoing down or a browser having navigated away from a page.

1006 is a reserved value and MUST NOT be set as a status code in aClose control frame by an endpoint. It is designated for use inapplications expecting a status code to indicate that theconnection was closed abnormally, e.g., without sending orreceiving a Close control frame.

1011 indicates that a server is terminating the connection becauseit encountered an unexpected condition that prevented it fromfulfilling the request.

我已经在服务器上使用 Wireshark 验证了传出的关闭代码。

作为从服务器向客户端传递信息的一种方式,关闭代码是否不可靠?在关闭 websockets 之前,我是否需要在传递此信息的应用层实现某些操作?

最佳答案

这只是一个猜测,但您列出的 WebSocket 客户端可能无法正确实现关闭握手。

你为什么不试试 nv-websocket-client 看看发生了什么? onDisconnected库的监听器接口(interface) ( WebSocketListener ) 的方法定义如下。

void onDisconnected(
WebSocket websocket,
WebSocketFrame serverCloseFrame,
WebSocketFrame clientCloseFrame,
boolean closedByServer);

第二个参数serverCloseFrame是服务器发送给客户端的关闭帧,第三个参数clientCloseFrame是客户端发送给服务器的关闭帧。在正常情况下,按照规范的要求,两个相近的帧的载荷是相同的。

关于websocket - 为什么客户端 websocket 关闭代码与服务器代码不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32525088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com