一、socket的由来
为了实现客户端和服务器端的双向通信,最开始想出的解决办法是不断的定时请求,但是耗流量多,也会消耗CPU的利用率。如图:

后来又想出了改进版的长轮询Long polling,就是客户端发送HTTP给服务器之后,看有没有新消息,如果没有新消息,就一直等待。当有新消息的时候,才会返回给客户端。在某种程度上减小了网络带宽和CPU利用率等问题。但还是不理想。如图:

最后WebSocket出现了,即实现了客户端和服务器端的双向通信,而且协议的头部又没有HTTP的Header那么大,最重要的是效率也大幅提高。如图:

网友评论