Send larger messages than 126 bytes websockets

后端 未结 2 884
生来不讨喜
生来不讨喜 2020-12-22 09:07

Now I\'m working on Websockets, I\'m new in that, I finally can send a message of 126 bytes, but I need send longer messages but when I try the connection is closed automati

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-22 09:37

    Your code to write the message length needs to be extended. The extended payload in the data framing diagram of the protocol spec shows what's missing.

    For messages up to 125 bytes, your code is correct.
    For messages > 125 but <= 65536 bytes, you need to write 3 bytes - the first byte is 126; the following 2 bytes give the message length.
    For messages > 65536 bytes, you need to write 9 bytes - the first byte is 127; the following 8 bytes give the message length.

提交回复
热议问题