WebSocket (3)--Introduction to WebSocket protocol

Source: Internet
Author: User

[This article is original, if reproduced, please indicate the source http://blog.csdn.net/yl02520/article/]

WebSocket protocol is a two-way communication protocol, which is based on TCP and transmits data via TCP as HTTP, but it differs from HTTP by two points: 1. WebSocket is a two-way communication protocol, after the establishment of the connection, the WebSocket server and Browser/ua can actively send or receive data to each other, like a socket, The difference is that WebSocket is a simple protocol for simulating sockets based on the web; 2. WebSocket requires a handshake connection, similar to TCP It also requires a handshake connection between the client and the server, and the connection succeeds to communicate with each other.

Here is a simple sequence diagram to create a handshake:

Here is a brief description of the WebSocket handshake process.

When the Web application calls the new WebSocket (URL) interface, browser begins the process of establishing a handshake connection with the webserver of the address URL.

1. Browser with the WebSocket server through TCP three handshake to establish a connection, if this connection failed, then the subsequent process will not be executed, the Web application will receive an error message notification.

2. After TCP establishes the connection successfully, BROWSER/UA transmits the WebSocket supported version number through the HTTP protocol, the Protocol's Word version number, the original address, the host address and so on some column fields to the server side.

For example:

Get/chat http/1.1
Host:server.example.com
upgrade:websocket
connection:upgrade
sec-websocket-key:dghlihnhbxbszsbub25jzq==
origin:http://example.com
sec-websocket-protocol:chat, Superchat
sec-websocket-version:13

3. The WebSocket server receives Browser/ua to send the handshake request, if the packet data and the format is correct, the client and the server side protocol version number matches and so on, accepts this handshake connection, and gives the corresponding data reply, similarly replies The data packet also uses the HTTP protocol transmission.

http/1.1 switching Protocols
upgrade:websocket
connection:upgrade
sec-websocket-accept: s3pplmbitxaq9kygzzhzrbk+xoo=
Sec-websocket-protocol:chat

4. Browser received the server replies to the packet, if the packet content, format is not a problem, it means that the connection is successful, triggering OnOpen message, at this time, Web developers can at this time through the Send interface to send data to the server. Otherwise, the handshake connection fails, and the Web application receives a ONERROR message and knows why the connection failed.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.