简体   繁体   English

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

[英]Why do client websocket close codes not match the server code?

I have a Spring Boot Tomcat server that is handling websocket connections from clients that are using: 我有一个Spring Boot Tomcat服务器,它正在处理来自使用以下客户端的websocket连接:

  1. SocketRocket SocketRocket
  2. Tyrus 泰鲁斯

I find that the close code provided by the server is often not the close code read by the client. 我发现服务器提供的关闭代码通常不是客户端读取的关闭代码。

For SocketRocket, I close the websocket at the server with code 1000, and the client often reads 1001. 对于SocketRocket,我用代码1000关闭服务器上的websocket,而客户端通常读取1001。

For Tyrus, I close the websocket with code 1011, and the client reads either 1006 or 1011. 对于Tyrus,我用代码1011关闭了websocket,客户端读取了1006或1011。

Descriptions of close codes from RFC 6455 : RFC 6455中关闭代码的描述:

1000 indicates a normal closure, meaning that the purpose for which the connection was established has been fulfilled. 1000表示正常关闭,表示已建立连接的目的已实现。

1001 indicates that an endpoint is "going away", such as a server going down or a browser having navigated away from a page. 1001指示端点正在“离开”,例如服务器关闭或浏览器已离开页面。

1006 is a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. 1006是保留值,端点不得在“关闭”控制帧中将其设置为状态代码。 It is designated for use in applications expecting a status code to indicate that the connection was closed abnormally, eg, 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. 1011表示服务器正在终止连接,因为它遇到了阻止其满足请求的意外状况。

I have verified the outgoing close codes using Wireshark at the server. 我已经在服务器上使用Wireshark验证了传出关闭代码。

Is the close code just unreliable as a means of passing information from the server to the client? 作为将信息从服务器传递到客户端的一种手段,关闭代码是否仅仅是不可靠的? Do I need to implement something at the application layer that passes this information before closing websockets? 我需要在关闭WebSocket之前在应用程序层实现传递此信息的功能吗?

This is just a guess, but the WebSocket clients you listed may not implement the closing handshake correctly. 这只是一个猜测,但是您列出的WebSocket客户端可能无法正确实现关闭握手。

Why don't you try nv-websocket-client to see what's happening? 您为什么不尝试使用nv-websocket-client来查看发生了什么? onDisconnected method of the library's listener interface ( WebSocketListener ) is defined as below. 库的侦听器接口( WebSocketListener )的onDisconnected方法定义如下。

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

The second argument serverCloseFrame is the close frame which the server sent to the client, and the third argument clientCloseFrame is the close frame which the client sent to the server. 第二个参数serverCloseFrame是服务器发送到客户端的关闭帧,第三个参数clientCloseFrame是客户端发送到服务器的关闭帧。 In normal cases, as required by the specification, payloads of the two close frames are identical. 在正常情况下,按照规范要求,两个闭合帧的有效载荷是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM