简体   繁体   English

socket.io中的延迟发射率回调

[英]Latency emittance callbacks in socket.io

I have the following communication in a socket.io project 我在socket.io项目中有以下通信

// Server side
socket.on('pattern', (value, callback) => {
   //... run some code
   callback(...)
})

// Client side
client.emit('pattern', value, (response) => {
   // ....process response
})

How does this logic stand up against latency factors? 这种逻辑如何抵抗延迟因素? eg :- 例如:-

  1. If the emittance were done twice on the client side( same client) would the callback be called twice? 如果在客户端(同一客户端)上完成两次发射,是否将调用两次回调? Would the second event nullify the first( not talking about value)? 第二个事件会使第一个事件无效(不谈论价值)吗?
  2. What would be the benefits of using this logic pattern instead of doing( especially where latency is involved):- 使用这种逻辑模式而不是这样做有什么好处(尤其是在涉及延迟的情况下):

    // Server side socket.on('pattern', (value) => { //... run some code socket.emit('callback', response) }) //服务器端socket.on('pattern',(value)=> {// ...运行一些代码socket.emit('callback',response)})

     // Client side client.on('callback', (response) => ....) client.emit('pattern', value) 

How do you handle latency in these scenarios? 在这些情况下如何处理延迟?

If the emittance were done twice on the client side( same client) would the callback be called twice? 如果在客户端(同一客户端)上完成两次发射,是否将调用两次回调?

Yes. 是。 Every event you send from the client is individually delivered to the server so your event handler on the server would get called for each separate message that the client sent. 您从客户端发送的每个事件都单独传递到服务器,因此服务器上的事件处理程序将针对客户端发送的每个单独的消息而被调用。

Would the second event nullify the first( not talking about value)? 第二个事件会使第一个事件无效(不谈论价值)吗?

No. Your code would first process the first event and then it would be separately called to process the second event. 不会。您的代码将首先处理第一个事件,然后将单独调用它以处理第二个事件。 What happens when your code gets two separate events with different data is completely up to your code (which you do not show). 当您的代码获得两个具有不同数据的单独事件时,将完全由您的代码决定(您不显示)。 But, socket.io will deliver them both and call your code with each separate piece of data. 但是,socket.io将同时提供它们并使用每个单独的数据段调用您的代码。 It's up to your code how to handle that. 您的代码如何处理。

I don't understand your #2 question. 我不明白您的第二个问题。 In general, if you can combine multiple pieces of data into one transmission, that is better than sending multiple transmissions (simply less overhead). 通常,如果您可以将多个数据组合到一个传输中,那将比发送多个传输更好(开销更少)。 That applies to all networking and is not just specific to socket.io. 这适用于所有联网,而不仅限于socket.io。

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

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