开发者

How do I make sure a message gets to its destination using WebSockets?

开发者 https://www.devze.com 2023-02-01 17:29 出处:网络
I\'m still trying to figure out WebSockets. I\'m sending over data from the javascript client to the python server as JSON strings,开发者_开发百科 but they arrive fragmented.

I'm still trying to figure out WebSockets. I'm sending over data from the javascript client to the python server as JSON strings,开发者_开发百科 but they arrive fragmented.

How can I make sure I've received the entire message before I start to parse it?


You need to read up on socket programming in general.

Reading some data from a websocket does not mean you've received everything the other side wanted to send.

Ideally you'd prefix your messages with a header that contains the size of the payload. Then after you read the header (say, terminated with LF, or being a fix 4 bytes, etc) you can figure out exactly how many more bytes to read to get the full message.

Anything you read after that becomes your next header. Etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号