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 @   lightsong  阅读(774)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2018-05-26 Flask 入门
千山鸟飞绝,万径人踪灭
点击右上角即可分享
微信分享提示