简体   繁体   English

Firefox下的Websockets延迟

[英]Websockets delay under Firefox

I've created a simple WebSockets server which sends a tiny packet every 20 ms, just for testing purposes. 我创建了一个简单的WebSockets服务器,该服务器每20毫秒发送一个微小的数据包,仅用于测试目的。 Then I wrote a simple browser client in pure WebSocket api, which connects to the server and prints Date.now() every time it receives a packet. 然后,我用纯WebSocket api编写了一个简单的浏览器客户端,该客户端连接到服务器并在每次接收到数据包时打印Date.now()

var ws = new WebSocket('ws://localhost:8080');
ws.onmessage = function(){ console.log(Date.now()); };

When I've launched the client in Google Chrome, the delay between the packets was consistent and always between 19 and 21 ms. 在Google Chrome浏览器中启动客户端后,数据包之间的延迟是一致的,并且始终在19到21毫秒之间。 When I've launched it under Firefox... uhh, delay was jumping between 3 and 114 ms. 当我在Firefox下启动它时...呃,延迟在3到114毫秒之间跳跃。 What's wrong with the Firefox and what's the workaround? Firefox怎么了?解决方法是什么? Is there some kind of buffering enabled by default? 默认情况下是否启用某种缓冲?

Tests were done on localhost under Linux, Firefox version is 38.0.5, packet size was 4 bytes, no additional code was running so garbage collector wasn't the issue. 测试是在Linux下的localhost上完成的,Firefox版本为38.0.5,数据包大小为4字节,没有其他代码在运行,因此垃圾收集器不是问题。 I've also launched both browsers simultaneously, results were the same. 我也同时启动了两个浏览器,结果相同。

After lots of testing, the result is quite funny - console.log implementation under Firefox is the root of all evil. 经过大量测试,结果非常有趣-Firefox下的console.log实现是万恶之源。 Calling it frequently results in short browser hangs which makes all the timers and events go out of sync. 频繁调用它会导致浏览器短暂挂起,这会使所有计时器和事件不同步。 When debugging timed events under Firefox, it's currently much better to store logs in an array and print it later. 在Firefox下调试定时事件时,目前最好将日志存储在数组中,然后再打印。

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

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