简体   繁体   English

websocket是否发送和接收完整消息?

[英]Do websockets send and receive full messages?

I am new to web development and I am trying to write a simple web game using python and django for my server with some javascript for the front end graphics. 我是Web开发的新手,我正在尝试使用python和django为我的服务器编写一个简单的网络游戏,并为前端图形编写一些javascript。 I am using the channels package to use websockets for the communication and I have a little question about websockets. 我正在使用channels包来使用websockets进行通信,但是我对websockets有点疑问。

In python, I know that when we use sockets, the message isn't guaranteed to be sent fully. 在python中,我知道当我们使用套接字时,不能保证消息会完全发送。 For example, if I use socket.send in python, I may not send the entire message and in that case I need to resend what is left, and on the receiving side, I am not guaranteed to get the full message using socket.recv (assuming the bufsize argument is big enough for the full message), so I have to keep calling recv until I get the entire message. 例如,如果我在python中使用socket.send ,则可能不会发送完整的消息,在这种情况下,我需要重新发送剩余的消息,并且在接收方,我不能保证使用socket.recv获得完整的消息。 (假设bufsize参数足以容纳完整的消息),因此我必须继续调用recv直到获得完整的消息。

My question is: Is it the same with websockets or not? 我的问题是:websockets是否相同?

I tried looking for this in the MDN docummentation and in google, but I couldn't find any information on this. 我尝试在MDN文档和Google中寻找此内容,但找不到任何相关信息。 In addition, every example I have seen online didn't take this into account, so it seems like what I send with socket.send (in javascript) is exactly what I get on the other end and vice versa. 另外,我在网上看到的每个示例都没有考虑到这一点,所以看来我用socket.send发送的socket.send (在javascript中)恰好是我在另一端得到的,反之亦然。

Is it true? 是真的吗 Can I assume that the message I send with socket.send on the client side will be received fully in one event on the server side (which in my case is a websocket consumer ) and the other way around: if the server sends a message to the client, will it be received fully in one Message event on the client side in javascript? 我是否可以假设我在客户端通过socket.send发送的消息将在服务器端的一个事件(在我的情况下是websocket使用者 )中被完全接收,而在另一种情况下:如果服务器将消息发送到客户端,是否会在javascript中的一个Message事件中完全收到它?

Thanks in advance. 提前致谢。

You can assume that the message will be received if the client is connected. 您可以假设如果连接了客户端,则会收到该消息。 And visa versa, that the server will receive the message it is listening. 反之亦然,即服务器将收到它正在侦听的消息。

Django Channel's are reliably unreliable. Django Channel确实不可靠。

Calling receive on these channels does not guarantee that you will get the messages in order or that you will get anything if the channel is non-empty.

https://channels.readthedocs.io/en/latest/channel_layer_spec.html#channel-semantics https://channels.readthedocs.io/zh-CN/latest/channel_layer_spec.html#channel-semantics

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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