WebSocket and sockets

Source: Internet
Author: User
Tags socket error

Original: WebSocket and socket

WebSocket and sockets

Tags:websocket and sockets

Introduction: Many friends want to know the connection and the difference between websocket and socket, here is what you want

First to a picture collected before, I see this picture really is laughed, then also gave my friend door forwarded a bit, do not know you smiled.

After reading the above figure, it should be guessed that they do not have any substantial connection between, of course, in addition to the name is a bit the same, the name of the article after the origin can refer to read

Socket

The English socket means socket, the socket in the network is an abstract interface that can be understood as both ends of the connection in the network. Often called a socket interface, the meaning of encapsulation in the transport layer masks the complexity of the transport layer. It is not an agreement, it is to make it easier for you to use a layer of abstraction generated by the Transport layer protocol. The socket function is available in most mainstream programming languages. Let's take PHP to illustrate

<?php  1 . socket_create   () -Creates a socket 2 . socket_accept   () -accepts a connection on a socket (receive) 3 . socket_bind   () -binds the socket to the IP and port number 4 . socket_connect   () -establishes a connection 5  with a socket. socket_listen   () -listens for  a connection on a socket (listening) 6  . socket_last_error   () -Returns The last error on the socket 7 . socket_strerror   () -return  a string describing a socket error ?>  

We can use these functions to establish a connection to implement communication functions. We'll talk about the socket.

WebSocket

Said WebSocket know some people may feel some tall on the feeling, its birth and some stories can be said, probably in the world to abandon the HTML, and then a group of people are not convinced (do not want to give up), want to continue to promote the development of HTML, and they have developed some other network standards, and is officially received. And WebSocket is one of them, in order to create a two-way communication (full-duplex) protocol, as a substitute for the HTTP protocol, to solve the HTTP-based long-polling technology can not solve (or solve the less beautiful) problem. And this fellow is not called WebSocket at the beginning, it seems to be called WebConnect, and finally an engineer suggested that either we call WebSocket, then ... Well, that's the story. Since he is a substitute for HTTP, let's first look at the connection and differences between it and HTTP (or the Long connection to HTTP).

WebSocket and HTTP 1.1 contact

First, both are application-level protocols, and WebSocket need to borrow HTTP 101 switch protocol to achieve protocol conversions when establishing a connection, in order to establish a WebSocket connection, the client browser first initiates an HTTP request to the server, This request differs from the usual HTTP request, including some additional header information, where the additional header information "Upgrade:websocket" and "Connection:upgrade" indicate that this is an HTTP request to request a protocol upgrade. The server side resolves these additional header information and then generates the reply message back to the client, the client and server side of the WebSocket connection is established, after the connection is established, and HTTP is not related, the two sides can pass through this connection channel free information.
Of course, it is possible that the server does not support WebSocket, then honestly use HTTP bar, most of the current browser and server has supported WebSocket.

Paste a simple websocket client JS code

<Script type="Text/javascript">    //syntax var Socket = new WebSocket (URL, [protocol]);    varWs= New WebSocket("Ws://localhost:6688/send");    //Trigger when connection is established    ws.OnOpen = function(EVT){        Console.Log("Connection open ...");        ws.Send("Hello websockets!");    };    //Trigger when message is received    ws.OnMessage = function(EVT){        Console.Log("Received Message:" + evt.Data);        ws.Close();    };    //close connection trigger    ws.OnClose = function(EVT){        Console.Log("Connection closed.");    };    //Trigger When communication error occurs    ws.onerror = function(EVT){        Console.Log("Connection Error.");    };    //Check whether the browser supports WebSocket    if(typeofWebSocket!= ' undefined '){        Alert("Your browser supports websocket!");    }Else{        //browser does not support WebSocket        Alert("Your browser does not support websocket!");    }</script>
WebSocket and HTTP 1.1 differences

Let's take a look at his format:

//一个WebSocket连接始于握手(handshake)GET /chat HTTP/1.1Host: server.example.comUpgrade: websocketConnection: UpgradeSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==Origin: http://example.comSec-WebSocket-Protocol: chat, superchatSec-WebSocket-Version: 13

Explain the above state:
The first two lines are the same as the starting line for the HTTP request, and the following header fields are the ones that really work in the WS handshake.

    • Upgrade:websocket
      Upgrade is the header field in HTTP1.1 that defines the conversion protocol . It means to upgrade (convert) to a protocol (if the server supports it).

    • Connection:upgrade indicates an upgrade agreement to be made

    • Sec-websocket-key: Used to send to the server to filter unexpected requests (such as manually filling out some information in the header, but itself does not want to upgrade to WebSocket. At this time, because Sec-websocket-key and some related items are forbidden to manually set, so you can filter out the unexpected situation.

    • Origin: Used safely to prevent cross-site attacks, this is typically used by browsers to identify the original domain.

    • Sec-websocket-protocol: A list of the sub-protocols supported by the client.

    • Sec-websocket-version: The version of the WS protocol that is supported by the client.

//服务端应答handshake 101表示切换HTTP/1.1 101 Switching ProtocolsUpgrade: websocketConnection: UpgradeSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=Sec-WebSocket-Protocol: chat

The above is the message difference, and there are some other features

    • WebSocket connection must be a direct connection (this I also need to study carefully, not very thorough, if a friend can help me understand understanding will be greatly appreciated.) I'll look at the way of communication and the format of the data frame.
    • after the WebSocket connection is established, both sides of the communication can send data to the other party at any time (that is, full duplex, which is the most important).
    • After the WebSocket connection is established, the transmission of the data is transmitted using frames, unlike the request.

Because the expansion is too long, you can also learn more about yourself.

WebSocket full-duplex and HTTP

In HTTP, a request corresponds to a response, Early HTTP1.0 each HTTP connection needs to open a TCP connection, after a request, the server generates an answer request, this time the HTTP connection is over, while the TCP connection is closed, a duplicate TCP connection is a waste of resources, the active shutdown of the TCP connection will also occur when the time _wait status, continue to occupy resources for a while (can look at an article TCP connection and time_wait, Close_waite)
This situation has been improved in HTTP1.1, making it possible to have a keep-alive, that is, in an HTTP connection, you can send multiple request, receive multiple response, can reduce the number of times to establish and dismantle TCP connections, thus reducing the time_ Wait state of the connection, however, if set keep-alive timeout time, such as Nginx is keepalive_timeout, a period of time without communication time-out after the server actively shut down the connection may also cause the server to appear time_wait state, It is also important to set this time-out if you do not set a time-out period that also causes a certain amount of wasted resources, which are used to connect without sending data.

In essence, although the HTTP1.1 can maintain a persistent connection, but it is still not full-duplex, because the server is not able to actively send messages to the client, Ajax polling method, although it can achieve websocket full duplex similar effect, but will cause a lot of waste of resources.

Data frame format
 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-------+-+-------------+-------------------------------+ | F| r| r| r| opcode| m|    Payload Len | Extended Payload Length | | i| s| s|  s| (4) |     a|             (7) | (16/64) | | n| v| v|       v| |             s|   |       (if payload len==126/127) | | |1|2|3| |             k|                               | |     +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + | Extended payload length continued, if payload len = = 127 |                               + - - - - - - - - - - - - - - - +-------------------------------+ | | Masking-key, if MASK set to 1 | +-------------------------------+-------------------------------+ |          Masking-key (continued) | Payload Data |                +-----------------------------------------------+: Payload Data continued ... : + - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - + | Payload Data Continued ... | +---------------------------------------------------------------+
    1. From left to right, the unit is bit. For example, FIN, RSV1, RSV2, RSV3 each occupy 1 bits, opcode occupy 4 bits.
    2. The content includes identification, operation code, mask, data, data length, and so on. (The next section will expand)
Individual flag bits

Refer to the above data frame format for the meaning of each field, important field bold

  • FIN: (finish) 1 bits.
    If it is 1, this is the last shard of the message (fragment), or 0, which means it is not the last shard.

  • RSV1, RSV2, RSV3: Each accounted for 1 bits.
    This must be 0 if the websocket extension is not used. When the client, server-side negotiation takes the websocket extension, these three flags can be 0, and the meaning of the value is defined by the extension. If a non-zero value is present and the websocket extension is not used, the connection error occurs.

  • Opcode:4 a bit.

    The operation code defines the interpretation of the "load data", and the value of opcode determines how the subsequent data loads should be parsed (payload). If an unknown opcode is received, the receiving endpoint should be disconnected (fail the connection). The optional operating code is as follows:

    %x0: Represents a continuation frame. When the opcode is 0 o'clock, it means that the data is fragmented, and the data frame currently received is one of the data shards.
    %X1: Indicates that this is a text frame
    %X2: Indicates that this is a binary frames (frame)
    %X3-7: Reserved operation code for subsequent defined non-control frames (some codes are reserved for expansion in the general protocol).
    %X8: Indicates disconnection/shutdown of the connection.
    %X9: Indicates that this is a ping operation.
    %xa: Indicates that this is a pong operation.
    %XB-F: Reserved operation code for subsequent defined control frames (some codes are reserved for expansion in the general protocol).

  • Mask:1 a bit.
    Indicates whether the data payload is to be masked. When sending data from the client to the server, it is necessary to mask the data, mask needs to have a value for the 1,masking-key (Mask key) field, and when sending data from the server to the client, it does not need to mask the data, the mask needs to be 0. If the data received by the server is not masked, the server needs to be disconnected.

  • Payload Length: The size of the data payload, in bytes. is a 7-bit, or 7+16 bit, or 1+64 bit.

    Assuming that the value of payload length is x, if
    X is 0~126: the length of the data is x bytes.
    X is 126: Subsequent 2 bytes represent a 16-bit unsigned integer, and the value of the unsigned integer is the length of the data.
    X is 127: The subsequent 8 bytes represent a 64-bit unsigned integer (the highest bit is 0), and the value of the unsigned integer is the length of the data.
    The above-defined load-length approach is a common method in network protocols that enables flexible extension of data length definitions.

  • masking-key:0 or 4 bytes (32 bits)
    All data frames transferred from the client to the server, the data payload is masked, mask is 1, and carries 4 bytes of Masking-key. If mask is 0, there is no masking-key.

  • Payload Data: (Payload x+y bytes)
    Load data: Includes extended data, application data. Where the data x bytes are extended, and the data y bytes are applied. The length of the load data, not including the length of the mask key.

  • Extension data (extended x bytes):
    If the extension is not negotiated, the extended data data is 0 bytes. All extensions must declare the length of the extended data, or how to calculate the length of the extended data. In addition, how the extension is used must be negotiated during the handshake phase. If the extended data exists, then the payload data length must include the length of the extended data.

  • Application data (application y bytes):
    Any application data after the extended data (if there is extended data) occupies the remaining position of the data frame. The length of the payload data minus the length of the extended data will be applied.

Ping Pang Heartbeat

Connection resources are wasted for connections that have no data interaction for a long time. However, there are some scenarios where the client and the server do not have data connections for a long time, but still need to remain connected. This time, you can use the heartbeat to achieve. (I don't know why the heartbeat is Ping, pang)

    1. Ping
    • Ping frame operation code (opcode) 0x9. Can contain "app data
    • When a ping frame is received, the receiver must send a pong frame in the response unless it has already received a closed frame. It should respond as quickly as possible to pong frames. (also used to verify that remote endpoints are responsive)
    1. Pong
    • Pong frame operation code (opcode) 0x9.
    • A pong frame must be portable and the same data in the ping frame being responded to
    • If another ping arrives at the server and the server has not yet responded, the ping frame that arrives at the same origin can only respond to the latest ping frame,
    • You can also send a pong frame without receiving a ping. This acts as a one-way heartbeat (Heartbeat), and the other side does not need to respond.

WebSocket and sockets

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.