Stay Hungry,Stay Foolish!

SocketIO平滑降级

Websocket

https://github.com/socketio/engine.io#goals

Websocket是SocketIO库依赖的B/S新特性,它有一些优点。

WebSocket based connections have two fundamental benefits:

  1. Better server performance
  • A: Load balancers
    Load balancing a long polling connection poses a serious architectural nightmare since requests can come from any number of open sockets by the user agent, but they all need to be routed to the process and computer that owns the Engine connection. This negatively impacts RAM and CPU usage.
  • B: Network traffic
    WebSocket is designed around the premise that each message frame has to be surrounded by the least amount of data. In HTTP 1.1 transports, each message frame is surrounded by HTTP headers and chunked encoding frames. If you try to send the message "Hello world" with xhr-polling, the message ultimately becomes larger than if you were to send it with WebSocket.
  • C: Lightweight parser
    As an effect of B, the server has to do a lot more work to parse the network data and figure out the message when traditional HTTP requests are used (as in long polling). This means that another advantage of WebSocket is less server CPU usage.
    1. Better user experience

      Due to the reasons stated in point 1, the most important effect of being able to establish a WebSocket connection is raw data transfer speed, which translates in some cases in better user experience.

      Applications with heavy realtime interaction (such as games) will benefit greatly, whereas applications like realtime chat (Gmail/Facebook), newsfeeds (Facebook) or timelines (Twitter) will have negligible user experience improvements.

 

平滑降级

但是在一些条件下,Websocket功能没有被开启。

Having said this, attempting to establish a WebSocket connection directly so far has proven problematic:

    1. Proxies
      Many corporate proxies block WebSocket traffic.

    2. Personal firewall and antivirus software
      As a result of our research, we've found that at least 3 personal security applications block WebSocket traffic.

    3. Cloud application platforms
      Platforms like Heroku or No.de have had trouble keeping up with the fast-paced nature of the evolution of the WebSocket protocol. Applications therefore end up inevitably using long polling, but the seamless installation experience of Socket.IO we strive for ("require() it and it just works") disappears.

这样会影响用户体验

From the user perspective, an unsuccessful WebSocket connection can translate in up to at least 10 seconds of waiting for the realtime application to begin exchanging data. This perceptively hurts user experience.

To summarize, Engine focuses on reliability and user experience first, marginal potential UX improvements and increased server performance second. Engine is the result of all the lessons learned with WebSocket in the wild.

架构设计上,协议可以在运行过程切换。

The main premise of Engine, and the core of its existence, is the ability to swap transports on the fly. A connection starts as xhr-polling, but it can switch to WebSocket.

支持的协议

Transports

  • polling: XHR / JSONP polling transport.
  • websocket: WebSocket transport.

 

Protocal fallback

https://github.com/socketio/engine.io-protocol

所以,当Websocket功能失效,系统自动降级为ajax轮训方式。

A connection always starts with WebSocket (if supported by the client).

If the connection cannot be established, the client will try to establish a SSE stream.

If the connection still fails, the client will use polling as a fallback (either XHR or JSONP).

 

posted @ 2020-05-26 15:22  lightsong  阅读(763)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel