[英]How do I make sure a message gets to its destination using WebSockets?
I'm still trying to figure out WebSockets. 我仍在尝试弄清楚WebSockets。 I'm sending over data from the javascript client to the python server as JSON strings, but they arrive fragmented.
我正在将数据作为JSON字符串从javascript客户端发送到python服务器,但它们分散在一起。
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. 从websocket读取一些数据并不意味着您已经收到了对方想要发送的所有信息。
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.
然后,在您阅读了标头(例如,以LF终止,或者是固定的4个字节,等等)之后,您可以准确地弄清楚还要读取多少个字节以获得完整的消息。
Anything you read after that becomes your next header. 之后读取的所有内容将成为您的下一个标题。 Etc.
等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.