聊天技术哪家强
实现原理层面大概有以下几种
轮询,常轮询,异步,长连接,新协议。
在线聊天技术与普通cs服务的区别
普通的cs服务都是同步的,即客户端一个request一直在等待response的回答。如果采用这种同步的模型去假设在线聊天服务,只能用轮询,这样的话做了很多无用功,很多请求都是无效的。这种技术基本被pass。
与轮询比较相似的技术是常轮询(long poll) ,
聊天技术无非轮询,异步,长连接 三种模式。
[long poll] wiki
Long polling is itself not a true push; long polling is a variation of the traditional polling technique, but it allows emulating a push mechanism under circumstances where a real push is not possible, such as sites with security policies that require rejection of incoming HTTP/S Requests.
With long polling, the client requests information from the server exactly as in normal polling, but with the expectation the server may not respond immediately. If the server has no new information for the client when the poll is received, instead of sending an empty response, the server holds the request open and waits for response information to become available. Once it does have new information, the server immediately sends an HTTP/S response to the client, completing the open HTTP/S Request. Upon receipt of the server response, the client often immediately issues another server request. In this way the usual response latency (the time between when the information first becomes available and the next client request) otherwise associated with polling clients is eliminated.[10]
For example, BOSH is a popular, long-lived HTTP technique used as a long-polling alternative to a continuous TCP connection when such a connection is difficult or impossible to employ directly (e.g., in a web browser);[11] it is also an underlying technology in the XMPP, which Apple uses for its iCloud push support.
[long poll]中文
long poll并不是真的push ,是传统poll技术的一个变种,但是通过实现了一种push,用例比如需要拒绝一些request情况的站点。
客户端正常的请求就ok了,只不过你要有预期: server 不会立即返回。
server收到后不会立即返回空数据,而是保持request是开的,直到有了response。
client收到之后一般会再次发起请求
通过这种方式,消除了响应延迟(收到可用数据与发起下次请求之间的时间)
例如,BOSH是一种流行的,长期存在的HTTP技术,当这种连接很难或不可能直接使用时(例如,在Web浏览器中),它被用作连续TCP连接的替代方案; [11]也是苹果公司用于其iCloud推送支持的XMPP中的一项基础技术。
long poll 与 Poll 的区别
long poll 需要服务器和客户端配合
- 服务器不会立即返回数据,直到有新消息
- 客户端收到请求之后要立即再发起请求
长连接
tcp连接不会断掉,http层面即,keep alive。
这样的话优先很明显。变成模型变得很简单,缺点是服务器为了维持这些链接要耗很多资源,而且能维持的连接的个数是有限的。
异步
应用追光,性能最强的技术方式。
服务器仍然采用传统的编程模型,不过把返回数据变为异步返回。
解决了上面维持链接性能的问题,又解决了请求无效的问题。
如何实现异步
- 关键字yield
- async/await 模型
- asyncio
- aiohttp
新协议
比较有名数 websocket协议,同时还有socketio协议。
目前应用比较广的有socketio协议。











网友评论