zoukankan      html  css  js  c++  java
  • WebSocket协议再认识

    WebSocket出现之前

    在线聊天室、在线客服系统、评论系统、WebIM等这些应用有一个共同点,就是用户不需要去刷新浏览器就能够从服务器获得最新的数据,这就用到了推送技术。

    WebSocket出现之前,实现推送一般有两种方式,polling与comet。

    1、polling是在特定的的时间间隔(如每1秒),由浏览器对服务器发出HTTP request,然后由服务器返回最新的数据给客户端的浏览器。这种方式下浏览器需要不断的向服务器发出请求,然而HTTP request的header是非常长的,里面包含的数据可能只是很小一部分,这样会占用很多的带宽和服务器资源。

    2、comet中普遍采用了长链接,这也会大量消耗服务器带宽和资源。在wikipedia有如下描述:

    Comet是一种用于web的推送技术,能使服务器实时地将更新的信息传送到客户端,而无须客户端发出请求,目前有两种实现方式,长轮询和iframe流。

    3、long polling和polling的区别(wikipedia给出的详细的解释):

    With long polling, the client requests information from the server exactly as in normal polling, except it issues its HTTP/S requests (polls) at a much slower frequency. If the server does not have any information available 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.

    WebSocket解决了什么问题

    wikipedia给出了下面的定义:

    WebSocket是HTML5开始提供的一种在单个 TCP 连接上进行全双工通讯的协议。

    同时注意到HTTP为半双工协议且消息冗长繁琐。为了解决HTTP协议效率低下的问题,HTML5定义了WebSocket协议,能更好的节省服务器资源和带宽并达到实时通讯。websocket.org的描述如下:

    HTML5 WebSockets provide an enormous reduction in unnecessary network traffic and latency compared to the unscalable polling and long-polling solutions that were used to simulate a full-duplex connection by maintaining two connections.
    HTML5 WebSockets-based applications place less burden on servers, allowing existing machines to support more concurrent connections.

    WebSocket与HTML5、HTTP

    1. HTML5 是一个很宽广的概念,是对大量新 API 的总称。
    2. 可以把 WebSocket 看成是 HTTP 协议为了支持长连接所打的一个大补丁。
    3. WebSocket和HTTP都是基于TCP的。客户端开始建立 WebSocket 连接时要发送一个 header 标记了 Upgrade 的 HTTP 请求,表示请求协议升级。

    WebSocket是如何工作的

    作为设计原则的一部分,协议明确指出WebSocket以一个HTTP连接作为其生命周期的开始,保证了完全向后兼容。

    1. 首先浏览器发送一个请求到服务器,指明它想从HTTP切换到WebSocket。客户端(浏览器)通过HTTP头部Upgrade字段表明其意愿。
    GET ws://echo.websocket.org/?encoding=text HTTP/1.1
    Origin: http://websocket.org
    Cookie: __utma=99as
    Connection: Upgrade
    Host: echo.websocket.org
    Sec-WebSocket-Key: uRovscZjNol/umbTt5uKmw==
    Upgrade: websocket
    Sec-WebSocket-Version: 13
    
    1. 如果服务器支持WebSocket,它将通过头部Upgrade字段表明其同意客户端(浏览器)的请求。
    HTTP/1.1 101 WebSocket Protocol Handshake
    Date: Fri, 10 Feb 2012 17:38:18 GMT
    Connection: Upgrade
    Server: Kaazing Gateway
    Upgrade: WebSocket
    Access-Control-Allow-Origin: http://websocket.org
    Access-Control-Allow-Credentials: true
    Sec-WebSocket-Accept: rLHCkw/SKsO9GAH/ZSFhBATDKrU=
    Access-Control-Allow-Headers: content-type
    

    WebSocket特点与性能

    1、特点总结
    1)单一的TCP连接,采用全双工模式
    2)对代理、防火墙和路由器透明
    3)无头部信息、cookie和身份验证
    4)通过ping/pong帧保持链路激活
    5)服务器可以主动传递消息给客户端,不再需要客户端轮询
    2、性能方面,请参考Benefits of WebSocket

    参考:WebSocketPush technologyHTTP持久连接websocket.orgpolling-vs-long-pollingWebSocket 是什么原理?为什么可以实现持久连接?

  • 相关阅读:
    subprocess
    bytes(str_, encoding="utf8")
    按文件生成时间 排序 批量与生成同步上传文件
    async
    http trigger 事件源是事件的生产者,函数是事件的处理者
    分片上传
    使用 FFmpeg 处理高质量 GIF 图片
    兴趣 主题 字段 二值化 多值并列属性 拆分 二值化
    打开 回收站
    shell如何查看单个或多个文件的行数或总行数
  • 原文地址:https://www.cnblogs.com/shuaihanhungry/p/5873025.html
Copyright © 2011-2022 走看看