当我注意到这种框架时,我正在使用Wireshark检查我的服务器和浏览器之间的websocket流量:
No. Time Source Destination Protocol Length Info
144342 8212.033150000 127.0.0.1 127.0.0.1 WebSocket 821 WebSocket Text [FIN]
Frame 144342: 821 bytes on wire (6568 bits), 821 bytes captured (6568 bits) on interface 0
Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00_00:00:00 (00:00:00:00:00:00)
Internet Protocol Version 4, Src: 127.0.0.1 (127.0.0.1), Dst: 127.0.0.1 (127.0.0.1)
Transmission Control Protocol, Src Port: http-alt (8080), Dst Port: 53749 (53749), Seq: 1132, Ack: 603, Len: 755
WebSocket
1... .... = Fin: True
.000 .... = Reserved: 0x00
.... 0001 = Opcode: Text (1)
0... .... = Mask: False
.111 1110 = Payload length: 126 Extended Payload Length (16 bits)
Extended Payload length (16 bits): 140
Payload
Text: {"type":"COLLABORATIVE_COMMANDS","remoteUser":"null","key":"1c78c08f-5d2d-445a-a63c-3a211d2f0336","messageBroadcast":{"smartPath":["null"]}}
WebSocket
1... .... = Fin: True
.000 .... = Reserved: 0x00
.... 0001 = Opcode: Text (1)
0... .... = Mask: False
.111 1110 = Payload length: 126 Extended Payload Length (16 bits)
Extended Payload length (16 bits): 329
Payload
Text [truncated]: {"type":"COLLABORATIVE_COMMANDS","remoteUser":"","key":"1c78c08f-5d2d-445a-a63c-3a211d2f0336","messageBroadcast":{"cameraInfos":{"target":{"x":0,"y":0,"z":0},"camPos":{"x":557.0133301398326,"y":159.5460628202445,"z":342.4
WebSocket
1... .... = Fin: True
.000 .... = Reserved: 0x00
.... 0001 = Opcode: Text (1)
0... .... = Mask: False
.111 1110 = Payload length: 126 Extended Payload Length (16 bits)
Extended Payload length (16 bits): 141
Payload
Text: {"type":"COLLABORATIVE_COMMANDS","remoteUser":"","key":"1c78c08f-5d2d-445a-a63c-3a211d2f0336","messageBroadcast":{"colourEditedMeshes":true}}
WebSocket
1... .... = Fin: True
.000 .... = Reserved: 0x00
.... 0001 = Opcode: Text (1)
0... .... = Mask: False
.111 1110 = Payload length: 126 Extended Payload Length (16 bits)
Extended Payload length (16 bits): 129
Payload
Text: {"type":"COLLABORATIVE_COMMANDS","remoteUser":"","key":"1c78c08f-5d2d-445a-a63c-3a211d2f0336","messageBroadcast":{"explode":"0"}}
这是否意味着我的数据包中有几个websockets?这怎么可能?
答案 0 :(得分:1)
如果您阅读了WebSocket规范RFC 6455,您将看到WebSocket数据包是成帧的,其中每个帧都有自己的标头和有效负载。请记住,TCP是一种流式传输。发送者和接收者没有关注TCP帧,他们正在关注这些帧内的有效载荷。 WebSocket发送方将发送一个WebSocket标头,后跟其有效负载,然后是下一个WebSocket标头及其有效负载,依此类推。 WebSocket接收器将读取WebSocket标头及其有效负载,然后读取下一个WebSocket标头及其有效负载,依此类推。通常,在TCP套接字上启用Nagle algorithm,它将根据需要拆分应用程序数据并将其组合到TCP帧中,以实现高效的网络传输。这是由对应用程序透明的TCP堆栈处理的。所以,是的,有可能看到多个WebSocket框架出现在单个TCP框架内,如果这是Nagle决定传输它们的方式。如果在短时间内发送WebSocket数据包,Nagle可能会合并它们,因此它只需要传输单个TCP帧而不是单独的TCP帧。