简体   繁体   English

在websocket中,如何防止缓慢的异步响应数据不正确的修改

[英]In websocket,how to prevent slow asyn response data incorrect modify

I'm using websocket and I need to send request. 我正在使用websocket,我需要发送请求。

In my case, I send the first request and then I send a second request before I get the response for the first request. 就我而言,我先发送第一个请求,然后再发送第二个请求,然后再获得第一个请求的响应。 The response for second request came back very quickly and then the response for the first request came. 对第二个请求的响应很快返回,然后对第一个请求的响应出现了。 At this moment, data from the later response modify the data incorrectly. 此时,来自后续响应的数据会错误地修改数据。

Is there any good ways to prevent this? 有什么好的方法可以防止这种情况发生?

Sounds like what you really want is to queue requests. 听起来您真正想要的是将请求排队。

Simplest way is to simply fire the 2nd one after the 1st one returns. 最简单的方法是在第一个返回后立即触发第二个。

A slightly more efficient mechanism is to fire both at the same time but keep track of which is which (this should be trivial). 一种稍微有效的机制是同时触发两个,但是要跟踪哪个是哪个(这应该是微不足道的)。

Even better, if you can, is to create a route that returns both bits of data you require. 如果可以的话,更好的办法是创建一条返回所需数据的位的路由。

You mention something about the data being wrong somehow, this is unrelated to firing off multiple requests but maybe that is the real problem here? 您提到某种程度上的数据错误 ,这与触发多个请求无关,但也许这才是真正的问题?

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

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