简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM