Jelle De Loecker
Jelle De Loecker

Reputation: 21955

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?

Upvotes: 1

Views: 147

Answers (1)

martona
martona

Reputation: 5914

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.

Upvotes: 1

Related Questions