1. Web的基本数据帧格式:
WebSocket在建立连接之后,通信的基本数据格式如下图(来源RFC6455-5.2),没有Http请求那么多固定的报文,且不用重复建立连接,所以通信效率高:
2. Websocket是如何管理连接的?
RFC6455-5.5给出了答案,协议定义了Control Frame。
WebSocket的控制帧有:Close、Ping、Pong。其中Close是发起关闭请求;Ping是通信发起方确认链路是否畅通的报文;Pong是通信接收方回应链路是否畅通的报文。
对应数据帧中的opcode位(Close 0x8, Ping 0x9, Pong 0xA).
3. Http与Websocket的区别与联系:
Http与websocket是两个完全不同的协议,都是基于TCP的。两者唯一的联系是WebSocket利用Http进行握手;具体说明请看RFC6455-1.7。
1.7. Relationship to TCP and HTTP _This section is non-normative._ The WebSocket Protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request. By default, the WebSocket Protocol uses port 80 for regular WebSocket connections and port 443 for WebSocket connections tunneled over Transport Layer Security (TLS) [RFC2818].