JavaScript:使用队列进行网络通信
我正在开发一个项目,其中客户端必须能够通过 WebSocket 与服务器进行通信。由于我们开发的应用程序对用户输入具有高度响应能力,因此我们决定让 WebWorker 通过网络进行所有通信,以便缓慢的连接不会中断 GUI。到目前为止效果很好。
现在我们考虑了如果网络速度慢且要发送的消息量很大时需要进行的优化。由于这些消息中的大多数仅用于同步其他一些客户端用户界面,因此我们可以在必要时删除其中一些消息。但要做到这一点,我们需要能够检测拥塞情况。
我们提出了队列的想法:每条要发送的消息都被推入队列中,WebWorker 除了永久迭代队列并发送它在队列中找到的所有消息之外什么也不做。这样,如果队列中有一定数量的元素(即消息发送太慢),我们可以让 Worker 采取不同的行为。这个想法简单明了,但实施起来似乎并非如此。
var ws;
var queue = new Array();
function processMessageQueue() {
while(true)
if(queue.length > 0) ws.send(queue.shift());
}
self.addEventListener('message', function(e) {
var msg = e.data;
switch(msg.type) {
case 'SEND':
queue.push(JSON.stringify(msg));
break;
case 'CREATE_WS':
ws = new WebSocket(msg.wsurl);
ws.onmessage = function(e) {
self.postMessage(JSON.parse(e.data));
}
processMessageQueue();
}
}, false);
正如您所看到的,工作线程在收到消息后立即创建 WebSocket。然后,它执行 processMessageQueue() 函数,该循环通过 WebSocket 发送数据来永久清空队列。现在,我的问题是似乎无法将消息推送到队列中。当“SEND”类型的消息到达但由于 Worker 太忙而无法处理任何事件时,这种情况应该会发生。因此它可以循环队列或推送消息,但不能两者兼而有之。
我需要的是一种以某种方式将数据推送到该队列上的方法。如果这不容易实现,我想知道是否有人可以想出另一种方法来找出消息何时到达比发送更快。有什么提示吗?
提前致谢!
I'm working on a project in which a client must be able to communicate with a server via WebSockets. Since the application which we develop as to be highly responsive on user input we have decided to let a WebWorker do all the communication over the network so that a slow connection cannot interrupt the GUI. That works fine so far.
Now we thought about optimizations which will be necessary if the network is slow and the amount of messages to send is high. Since most of these messages will be only to synchronize some other clients user interface we can drop some of them if necessary. But to do so we need the possibility to detect the situations when there is congestion.
We came up with the idea of a queue: Each message to be sent is pushed in a queue and the WebWorker does nothing else than permanently iterating over the queue and sending all the messages it finds there. With that we can later let the Worker act differently if there is a certain number of elements in the queue (i.e. messages are sent too slowly). The idea is simple and straightforward, however, the implementation doesn't seem to be.
var ws;
var queue = new Array();
function processMessageQueue() {
while(true)
if(queue.length > 0) ws.send(queue.shift());
}
self.addEventListener('message', function(e) {
var msg = e.data;
switch(msg.type) {
case 'SEND':
queue.push(JSON.stringify(msg));
break;
case 'CREATE_WS':
ws = new WebSocket(msg.wsurl);
ws.onmessage = function(e) {
self.postMessage(JSON.parse(e.data));
}
processMessageQueue();
}
}, false);
As you can see, the worker creates the WebSocket as soon as it received the message to do so. It then executes the function processMessageQueue() which is the loop which permanently empties the queue by sending their data via the WebSocket. Now, my problem is that there seems to be no possibility to push messages into the queue. That should happen when a message of type 'SEND' arrives but it cannot for the Worker is too busy to handle any events. So it can either loop over the queue or push messages, not both.
What I need is a way to somehow push data on this queue. If that is not easily possible I would like to know if anyone can think of another way to find out when messages arrive faster then they are sent. Any hints?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须给工作人员时间来处理 onmessage 事件。执行
while(true)
将占用所有处理时间,并且不允许执行任何事件。例如,您可以将其更改为
You have to give the worker time to handle the onmessage event. Doing
while(true)
will take all the processing time and not allow any events to be executed.You could for instance change it to