简体   繁体   English

如何使用WebSockets确保消息到达目的地?

[英]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.

相关问题 如何确保定义了该变量? - How do I make sure that this variable gets defined? 在打开新窗口之前,如何确保单击功能中的所有代码都已执行? - How do I make sure all of the code in an on click function gets executed before opening a new window? 如何使用JavaScript + jQuery确保URL是图像? - How do I make sure a URL is an image using JavaScript + jQuery? 使用是的,我如何确保 2 个字段需要不同 - Using Yup how do i make sure that 2 fields need to be different 在使用random()时,如何确保没有重复的数字? - How do I make sure there are no repeated numbers when using random()? 使用Javascript,如何确保日期范围有效? - Using Javascript, how do I make sure a date range is valid? 我如何确保当我的省略号被添加为lastindexof空间时,它也在添加之前检查其字母,而不是特殊字符 - How can I make sure that when my ellipsis gets added lastindexof space it also checks its alphabet before it gets added and not special chars 在将值传递给res.render之前,如何确保函数完全执行? - How do I make sure a function is completely executed before its value is passed to res.render? 将WebSockets与ReactJS一起使用时,不知道如何在客户端上呈现服务器消息 - Using WebSockets with ReactJS, do not know how to render server message on client 如何使用websockets将消息发送到后端 - How can I send my message using websockets to backend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM