socket.io - XHR 轮询与 flashsocket 和 websocket

发布于 2024-12-25 15:29:26 字数 260 浏览 1 评论 0原文

我使用node.js 和socket.io。我的 socket.io 连接速度有问题。在 Internet Explorer 和 Opera 中,我遇到连接速度问题。 - 当我使用 flashsocket 或 websocket 时。当我使用传输轮询模式时,XHR 连接速度很快 - 在所有浏览器(IE、FF、Chrome、Opera)中。

传输模式 - XHR-polling 和 flash/websocket 之间有什么区别?最好的交通方式是什么? socket.io如何优化连接速度?

I use node.js and socket.io. I have a problem with the connection speed with socket.io. In Internet Explorer and Opera I have a problem with the connection speed. - When I use flashsocket or websocket. When I use the mode of transport-polling XHR connection is fast - in all browsers (IE, FF, Chrome, Opera).

What is the difference between the mode of transport - XHR-polling and flash / websocket? What is the best mode of transportation? How to optimize the connection speed is socket.io?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深爱不及久伴 2025-01-01 15:29:26

如果 Web 浏览器之间的连接速度随着时间的推移有所不同,我会感到惊讶,但您会在 Internet Explorer 和 Opera 中看到初始连接延迟的原因是原生 WebSocket 支持不可用,因为它已经默认禁用。因此,如果您选择 FlashSocket,则在建立连接之前需要下载额外的 Flash 对象(SWF 文件)。

WebSockets 在 IE10 中引入,在 Opera 中可用,但默认情况下禁用。

传输模式 - XHR-polling 和 flash/websocket 之间有什么区别?

  • XHR 轮询 - 请参阅 http://en.wikipedia.org/wiki/Push_technology#Long_polling
  • FlashSocket 连接 - 使用 Flash Socket 对象建立与 WebSocket 服务器的连接,并使用 WebSocket 协议进行通信。这意味着 Flash 和 JavaScript 之间存在交互,并且还意味着需要下载额外的 Flash 对象(SWF 文件)。

最好的交通方式是什么?

适用于任何本机支持它的 Web 浏览器(Chrome、Firefox、Safari)的 WebSocket。如果 Flash 对象(SWF 文件)位于浏览器缓存中,则连接速度应该很快。如果不是的话就会有延迟。 XHR 长轮询是一个很好的解决方案,可以跨浏览器工作,但也有缺点:

  • 在轮询请求之间显示的数据可能会过时(陈旧)。
  • 与 WebSocket 使用的单个 TCP 连接相比,这是一种效率较低的连接方法,因为 HTTP 长轮询使用多个连接来模拟双向功能
  • HTTP 具有开销,这意味着根据请求以及每个后续请求发送额外的标头信息。

socket.io如何优化连接速度?

(我对 socket.io 还很陌生,这只是一个建议)

我会看看 配置 Socket.io 文档,看看是否可以根据连接的浏览器有条件地设置传输。根据您的经验,这可能是:

  • Chrome、Firefox、Safari - WebSockets
  • IE、Opera - XHR-Polling

I'd be surprised if the general speed of the connection over time was different between web browsers, but the reason you'll see a delay in the initial connection in Internet Explorer and in Opera is that native WebSocket support is not available as it's been disabled by default. So, if you choose FlashSocket then an additional Flash object (SWF file) will need to be downloaded before a connection is established.

WebSockets are being introduced in IE10 and in Opera they are available, but disabled by default.

What is the difference between the mode of transport - XHR-polling and flash / websocket?

  • XHR-polling - see http://en.wikipedia.org/wiki/Push_technology#Long_polling
  • FlashSocket connection - uses a Flash Socket object to establish a connection to the WebSocket server and communicates using the WebSocket protocol. This means there is interaction between Flash and JavaScript and also means an additional Flash object (SWF files) will need to be downloaded.

What is the best mode of transportation?

WebSockets for any Web Browser that natively supports it (Chrome, Firefox, Safari). If the Flash object (SWF file) is in the browser cache then connection should be fast. If it's not then there will be a delay. XHR Long-Polling is a good solution and will work cross browser but there are negatives:

  • between poll requests the data on display could be out of date (stale).
  • It's a less efficient connection method than a single TCP connection used by WebSockets since HTTP Long-Polling uses multiple connection to simulate bi-directional functionality
  • HTTP has an overhead which means additional header information is sent upon request and each subsequent request.

How to optimize the connection speed is socket.io?

(I'm pretty new to socket.io to this is just a suggestion)

I'd look at the configuring Socket.io docs and see if you can conditionally set the transports based on the browser that is connecting. Based on your experiences this could be:

  • Chrome, Firefox, Safari - WebSockets
  • IE, Opera - XHR-Polling
夜吻♂芭芘 2025-01-01 15:29:26

要减少连接时间,可以尝试使用“连接超时”参数来减少连接超时(默认为 10 秒)。

例如,要将连接超时减少到 1 秒:

socket = io.connect('http://your-site.com',{'connect timeout': 1000});

To reduce the time of connection, you can try to reduce the connect timeout (which is 10 seconds by default) using the "connect timeout" parameter.

For example, to reduce the connect timeout to 1 second:

socket = io.connect('http://your-site.com',{'connect timeout': 1000});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文