Recently, I used Rongyun to develop chat pages. Through packet capture, I found that The SDK of Rongyun uses WebSocket to communicate with the server, so I have a simple understanding of the implementation principle of WebSocket

Rongyun SDK documentation: docs.rongcloud.cn/v4/

WebSocket to HTTP

1. The HTTP protocol has not changed for WebSocket

2. WebSocket is a new protocol after HTTP

The two have intersection, but also different

4. WebSocket uses HTTP protocol to complete part of the handshake

The WebSocket handshake

Focus on the following fields through packet capture cloud WebSocket request

The above is to initiate the WebSocekt request packet capture, first pay attention to the more than HTTP values:

Upgrade: websocketConnection: Upgrade sec-websocket-key: Upgrade sec-websocket-key: AI0TsvW7jltfmNOF +1eSqg== // Base64, generated randomly by the browser. Provides basic protection in conjunction with sec-websocket-Accept of the subsequent server response. Sec-websocket-version: 13 // Indicates the WebSocket Version. If the server does not support the version, you need to return an sec-websocket-versionheader containing the version number supported by the serverCopy the code

Check Response Headers:

The Sec - WebSocket - Accept: OfEosYlCAcvV jdwbW33VU0B50k = / / security verification. Base64 (sha1($sec-websocket-Accept,' 258eafa5-e914-47DA-95ca-C5AB0DC85b11 ')) WebSocketUpgrade: websocketConnection: Upgrade Has been successfully upgradedCopy the code

alternative

Browsers don’t have a perfect alternative to WebSocket. The simulation can only be done with Ajax polling or long polling, both of which have performance issues and cost resources

Ajax polling, long training in rotation: zhuanlan.zhihu.com/p/25690011