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

标签 websocket spring-websocket tyrus socketrocket

我有一个 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 for which the connection was established has been fulfilled.

1001 indicates that an endpoint is "going away", such as a server going 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 a Close control frame by an endpoint. It is designated for use in applications expecting a status code to indicate that the connection was closed abnormally, e.g., without sending or receiving a Close control frame.

1011 indicates that a server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling 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/

相关文章:

java - Spring 使用 websocket handler bean 作为单例,尽管它的原型(prototype)范围

java - StompSessionHandler 不在 afterConnected block 中调用 handleFrame?

security - 如何将HTTPS重定向到不安全的WS?

spring - Spring 中使用 STOMP 的多个房间

c++ - Websockets 与 c++ asio 库奇怪的行为

node.js - 将 dgram (UDP) 与 Socket.IO 一起使用时,数据报的发送速度是多少? ( Node )

java - websocket session 示例不集群.. spring-session-1.2.2

java - 是否可以使用 SSL 运行独立的 Tyrus websocket 服务器?

Java8 安全 WebSocket 握手问题(Tyrus 和 Jetty)

java - 带 CDI 的 Tyrus WebSocket 服务器独立版