The WebSocket of HTML5

Source: Internet
Author: User

What is WebSocket? WebSocket A protocol for full-duplex communication on a single TCP connection. That is, WebSocket is a protocol.

More popular understanding, we can click here.

Part I: Features of WebSocket
    • The connection can be established through a TCP handshake . The HTTP protocol requires a handshake of three times.
    • The server in HTTP is always passive, that is, the server responds only if the client makes a request at a time. However, in the WebSocket protocol, the server can proactively pass data to the client. This avoids the problem of polling.
    • WebSocket requires both browser and server support to be used, and HTTP protocol is universally supported. And WebSocket is a new protocol , but currently for compatibility, must be built on the basis of HTTP to initiate the request, such as only with the WebSocket protocol name is no longer http: but WS:.
    • Similarly,WebSocket is based on the TCP protocol .

Part II: Basic knowledge

After we have obtained the WebSocket connection, we can send the data to the server via the Send () method and receive the data returned by the server through the OnMessage event . The following API is used to create a WebSocket object.

var Socket = new WebSocket (URL, [protocol]);

The first parameter is the URL that needs to be connected, and the latter parameter is optional, making an acceptable sub-protocol.

WebSocket related properties

That is, after we have created the socket object, it will have a readyState property (the same name as the Xhr property), with the following values:

0 indicates that the connection has not been established

1 indicates that the connection is established and can be communicated

2 indicates that the connection is shutting down

3 indicates that the connection is closed or the connection cannot be opened

WebSocket method

There are only two methods in WebSocket, one is to send data using a connection, namely Socket.send (), and the other is to close the connection, that is, Socket.close ().

Note: There is no close related method in XHR, because a response is requested one time, so that it does not need close.

WebSocket Events

  

Part III: WebSocket instances

When establishing a WebSocket connection, the client browser first sends an HTTP request to the server, which differs from the normal HTTP request because it contains the Upgrade:websocket in the header field ; This indicates that this is an HTTP request for an upgrade to the WebSocket protocol, the server resolves these additional header information and then generates the reply message back to the client, and the client and service side of the WebSocket connection is established. Both parties can use this connection to transmit information freely, and this connection will persist to the client or one side of the server to actively close the connection.

Finish

https://www.zhihu.com/question/20215561

The WebSocket of HTML5

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.