按接收顺序处理WebSockets消息 [英] Processing WebSockets messages in order of receiving
问题描述
我的应用程序的客户端部分需要严格按顺序处理WebSocket消息。不幸的是,每条消息的处理时间很长(大约3秒),因此在第一条消息结束之前会出现另一条消息。在几条消息之后,订单完全不同。如何在JavaScript中解决这个问题。
Client side part of my application needs to process WebSocket messages in strict order. Unfortunately each message is processed quite long (about 3 seconds), so another appears before the first one has ended. After few messages the order is totally different. How to solve this problem in JavaScript.
我想到了一个任务队列,但我不知道如何实现它以阻止我的Web应用程序的GUI。 / p>
I thought about a task queue, but I don't know how to implement it to not block GUI of my web app.
推荐答案
此时,浏览器中的WebSocket API不会公开基于框架的或流选项/ API。所以实际上没有选择来实现本地。
At this time, the WebSocket API in browsers does not expose a frame based or streaming option/API. So there is not really an option to accomplish this natively.
这意味着您需要在数据包中创建自己的逻辑/结构'从您的服务器发送到客户端。这应该是一个相对容易的任务,我可以考虑传输简化的 JSON字符串/对象,它可能在顶层有索引属性。
That means you need to create your own logic / structure within the data packets you're sending from your server to clients. That should be a relatively easy task, I could think of transmitting simpl JSON strings/objects which might have a indexed property on top level.
{
packetIndex: 0,
data: { }
}
并且您的客户端脚本需要查看每个到达的数据包并正确排序(跟踪已到达的数据包和保持+等待,如果数据包包含索引太高了抵达)
and your client script would need to look into each arriving packet and sort things out correctly (keeping track of arrived packets and "hold + wait" if a packet with a too high indexed arrived)
这篇关于按接收顺序处理WebSockets消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!