近期在神州,要用到这货走push,先自研 下 socket.io ,于是有如下分享。
了解socket.io 之前先看下官网的关系图:https://socket.io/docs/internals/#engine-io

- https://github.com/socketio/socket.io
- https://github.com/socketio/socket.io-client
- https://github.com/socketio/socket.io-parser
- https://github.com/socketio/socket.io-adapter
- https://github.com/socketio/socket.io-redis
- https://github.com/socketio/engine.io
- https://github.com/socketio/engine.io-client
- https://github.com/socketio/engine.io-parser
以上为 这些项目 对应的 关系图。
因此,学习socket.io
之前,了解 engine.io
相关的知识,方便深入理解。
本文 主要用 流程图 表述了 iOS 端 engine.io的 建立 长连接的逻辑。
主要方案是 基于websocket 协议去建立长连接。
备选的降级方案是 走http request 长轮询 去建立连接,默认 超时60s。重连间隔30s 。
websocket 与 http 请求长轮询 只用其中一种也都是可配置,通过 forcewebsockets 和 forcePolling 配置。
值得注意的是,就算是forcewebsockets 设置为NO,也有可能走 websocket。一旦在http轮询期间,发现websocket是可用,会进行fastUpgrade操作进行升级。
网友评论