websocket个人整合理解 与golang,gin应用

维基百科上的简介

WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API in Web IDL is being standardized by the W3C.

WebSocket is a different protocol from HTTP. Both protocols are located at layer 7 in the OSI model and depend on TCP at layer 4. Although they are different, RFC 6455 states that WebSocket “is designed to work over HTTP ports 80 and 443 as well as to support HTTP proxies and intermediaries” thus making it compatible with the HTTP protocol. To achieve compatibility, the WebSocket handshake uses the HTTP Upgrade header[1] to change from the HTTP protocol to the WebSocket protocol.

The WebSocket protocol enables interaction between a web browser (or other client application) and a web server with lower overheads, facilitating real-time data transfer from and to the server. This is made possible by providing a standardized way for the server to send content to the client without being first requested by the client, and allowing messages to be passed back and forth while keeping the connection open. In this way, a two-way ongoing conversation can take place between the client and the server. The communications are done over TCP port number 80 (or 443 in the case of TLS-encrypted connections), which is of benefit for those environments which block non-web Internet connections using a firewall. Similar two-way browser-server communications have been achieved in non-standardized ways using stopgap technologies such as Comet.

Most browsers support the protocol, including Google Chrome, Microsoft Edge, Internet Explorer, Firefox, Safari and Opera.

WebSocket是一种计算机通信协议,通过单个TCP连接提供全双工通信通道。 WebSocket协议在2011年由IETF标准化为RFC 6455,Web IDL中的WebSocket API由W3C标准化。

WebSocket是一种与HTTP不同的协议。这两个协议都位于OSI模型的第7层,并依赖于第4层的TCP。虽然它们不同,但RFC 6455规定WebSocket“设计为通过HTTP端口80和443工作,以及支持HTTP代理和中介”从而使其与HTTP协议兼容。为了实现兼容性,WebSocket握手使用HTTP Upgrade头[1]从HTTP协议更改为WebSocket协议。

WebSocket协议支持Web浏览器(或其他客户端应用程序)与Web服务器之间的交互,具有较低的开销,便于实现与服务器的实时数据传输。这可以通过为服务器提供标准化的方式来实现,而无需客户端首先请求将内容发送到客户端,并允许消息在保持连接打开的同时来回传递。通过这种方式,可以在客户端和服务器之间进行双向持续对话。通信通过TCP端口号80(在TLS加密连接的情况下为443)完成,这对于使用防火墙阻止非Web Internet连接的环境有利。使用诸如Comet之类的权宜之计技术,以非标准化方式实现了类似的双向浏览器 - 服务器通信。

大多数浏览器都支持该协议,包括Google Chrome,Microsoft Edge,Internet Explorer,Firefox,Safari和Opera。

与早期的轮询方式的优势

  • 轮循

    也叫Polling。使用ajax请求,每隔一段时间,向服务器发送请求。这种方案会频繁地与服务器通讯,每次通讯都是发送完整的http请求,如果服务器经常有数据变动,有回应还好,有时候发送的请求都是没有意义,都是在等服务器端的回应,而服务器又没有任何改变,所以这种方式很消耗网络资源,很低效。

  • ws

  1. WebSocket 解决了,通过第一个 HTTP request 建立了 TCP 连接之后,之后的交换数据都不需要再发 HTTP request了,使得这个长连接变成了一个真.长连接。

  2. WebSocket 还是一个双通道的连接,在同一个 TCP 连接上既可以发也可以收信息。

  3. 此外还有 multiplexing 功能,几个不同的 URI 可以复用同一个 WebSocket 连接。

(多路复用扩展(A Multiplexing Extension for WebSockets):这个扩展可以将WebSocket 的逻辑连接独立出来,实现共享底层的TCP 连接。每个WebSocket 连接都需要一个专门的TCP 连接,这样效率很低。多路复用扩展解决了这个问题。它使用“信道ID”扩展每个WebSocket 帧,从而实现多个虚拟的WebSocket 信道共享一个TCP 连接。)

Golang与websocket

下面使用到 gorilla/websocket 与GIN框架 实现websocket的一个demo

附上demo地址:https://github.com/516134941/websocket-gin-demo

猜你喜欢

转载自blog.csdn.net/af2251337/article/details/89535443
今日推荐