Introduction to WebSockets

WebSocket protocol is a new network protocol based on TCP. It implements full-duplex communication between the browser and the server - allowing the server to actively send information to the client.

The WebSocket protocol supports full- duplex communication between a client ( running untrusted code in a controlled environment ) and a remote host (opting in to the code's communication) . The security model used for this is the raw-based security model commonly used by web browsers. The protocol consists of an open handshake followed by message frames on the TCP layer. The goal of this technology is to provide a communication mechanism for browser-based applications that require two-way communication with the server (the server cannot rely on opening multiple HTTP connections (eg, using XMLHttpRequest or <iframe> and long polling)) .

Implementation principle


In the process of implementing a websocket connection, a websocket connection request needs to be sent through the browser, and then the server sends a response. This process is usually called "handshake". In the WebSocket API, the browser and the server only need to do a handshake, and then a fast channel is formed between the browser and the server. Data can be transferred directly between the two. In this WebSocket protocol, there are two major benefits for us to achieve instant services:
1. Header
The headers that communicate with each other are very small - only about 2 Bytes
2. Server Push
For server push, the server no longer passively receives the browser's request before returning data, but actively pushes it to the browser when there is new data.

Handshake Protocol Example


browser request
GET /webfin/websocket/ HTTP/1.1
Host: localhost
  Upgrade: websocket
Connection: Upgrade
  Sec-WebSocket-Key: xqBt3ImNzJbYqRINxEFlkg==
  Origin: http://server address
  Sec-WebSocket-Version: 13
server response
HTTP/1.1 101 Switching Protocols
  Upgrade: websocket
  Connection: Upgrade
  Sec-WebSocket-Accept: K7DJLdLooIwIG/MOpvWFB3y3FE8=
WebSocket uses http request for handshake, which has more content than normal http request. in,
Upgrade: websocket
Connection: Upgrade
Indicates that you want to upgrade the http protocol to the Websocket protocol.
Sec-WebSocket-Key is a base64 encoded value randomly generated by the browser to ask the server whether it supports WebSocket.
server returns
Upgrade: websocket
Connection: Upgrade
Tell the browser that the upcoming upgrade is the Websocket protocol
Sec-WebSocket-Accept is to concatenate the value of the request packet "Sec-WebSocket-Key" with the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", and then perform sha-1 operation on the concatenated string, Then base64 encoding is obtained. Used to describe itself as a WebSocket assistant server.
Sec-WebSocket-Version is the WebSocket protocol version number. The required version of RFC6455 is 13, and all previous draft versions should be deprecated.
For more handshake specifications, see RFC6455.

HTML5 Web Socket API


在HTML5中内置有一些API,用于响应应用程序发起的请求。基本API语句如下:

创建对象

1
var  ws =  new  WebSocket(url,name);
url为WebSocket服务器的地址,name为发起握手的协议名称,为可选择项。

发送文本消息

1
ws.send(msg);
msg为文本消息,对于其他类型的可以通过二进制形式发送。

接收消息

1
ws.onmessage = ( function (){...})();

错误处理

1
ws.onerror = ( function (){...})();

关闭连接

1
ws.close();

浏览器以及语言支持


所有主流浏览器都支持RFC6455。但是具体的WebSocket版本有区别。
php jetty netty ruby Kaazing nginx python Tomcat Django erlang

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325402886&siteId=291194637