socket.io - XHR polling vs flashsocket and websocket

前端 未结 2 1907
小鲜肉
小鲜肉 2020-12-24 07:04

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 u

相关标签:
2条回答
  • 2020-12-24 07:47

    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});
    
    0 讨论(0)
  • 2020-12-24 07:57

    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
    0 讨论(0)
提交回复
热议问题