Home > Web Front-end > H5 Tutorial > Detailed introduction to WebSocket API HTML5 specification translation

Detailed introduction to WebSocket API HTML5 specification translation

黄舟
Release: 2017-03-16 16:06:00
Original
2418 people have browsed it

1. Introduction

In order to allow the Web application to maintain two-way communication with the server process, here is the introduction of WebSocket interface.

2. Consistency requirements (omitted)

3. Terminology (omitted)

4.WebSocketInterface

(code omitted)

WebSocket(url, protocols)Constructorcan be passed in1-2 parameters. The first Rigel specifies the URL to connect to. The second parameter, if declared, can be a string or a string array . If it is a string, it is equivalent to an array containing only that string. If the second parameter is omitted, it is equivalent to an empty array [that is, no matter what is passed in the second parameter, it will be treated as a string array]. Each string in the array is a subprotocol name. The connection can only be established if the server declares that it has selected one of the subprotocols. All subprotocol names must meet the following requirements: These strings must contain the Sec-WebSocket-Protocol in the WebSocket protocol declaration header. Content①.

When the WebSocket() constructor is called, the user agent must perform the following steps:

(1) From urlParses out each component of URL to obtain the host, port, resource name and securityprotocol(host , port, resource name, and secure). If this process fails, a SyntaxError exception will be thrown and the following steps will be terminated.

(2) If secure is empty, but the original entry script has a pattern that is itself a security protocol, such as HTTPS , then throw SecurityError exception. (If secure is false but the origin of the entry script has a scheme component that is itself a secure protocol, e.g. HTTPS, then throw a SecurityError exception.)

(3) If the port used in the parameter is set to be prohibited from access by the user agent, then a SecurityError exception will be thrown. Ports 80 and 443 should not be blocked, including secure is# In the case of ##false443 port cannot be prohibited from access, secure is When true, the 80 port cannot be blocked from access [Here it means that there are two types of WebSocket In the form, ws does not have a security protocol, and the default port is 80, wss is with a security protocol, and the 443 port is used by default. However, even if the protocol used does not require a corresponding port, access to 443 cannot be prohibited. and 80 are the two ports].

(4) If the

protocols parameter is not declared, let it become an empty array. Otherwise, if the protocols parameter is a string, let it become an array containing the string.

(5) If any

variable in the protocols parameters is declared more than this, or does not meet the requirements of ①, then Throws the SyntaxError exception and terminates the following steps.

(6)Let

origin be the ASCII serialization of the origin of the entry script, converted to ASCII lowercase.

(7)Return a new

WebSocketObject, and continue to perform the following steps in the background program.

(8) Create a file containing the given

host, port, resource name, and secure and protocols list, and an empty extensions(extensions) list and origin#WebSocketconnect. To send the correct cookies header, you must use the Cookie header, the value of which is passed by the user Calculated from the stored cookie value and url. For these purposes, this is not an API without the HTTP protocol.

During the execution of the "WebSocketEstablishing Connection Algorithm", when the user agent verifies the server's response, if the status code received from the server is not 101, the user agent must terminate this WebSocket connection.

warn! Using HTTP here may cause serious security risks to the browser context. For example, imagine a host is a server set up using WebSocket on one path, with an open HTTP redirect pointing to another path . Suddenly, any script that is specified with a specific WebSocket URL can enter this service and communicate with other hosts on the Internet, even if the script checks the URL is the correct host name.

Note: If the WebSocket connection algorithm fails to execute, the WebSocket connection failure algorithm will be triggered. This algorithm will Execute the algorithm for closing the WebSocket connection, which will close the WebSocket connection and trigger close Event, the execution steps are as described below.

When the global object of the script is a Window object or an object instantiated through the WorkerUtils interface, This constructor must be visible. The

url attribute must be parsed from the URL passed to the constructor. The

readyState attribute represents the status of the connection. It has the following values:

CONNECTING, the corresponding value is 0, indicating that the connection has not been established;

OPEN, the corresponding value is 1, indicating WebSocketThe connection has been established and communication can be carried out;

CLOSING, the corresponding value is 2, indicating that the connection is undergoing a closing handshake, or the close() method has been called;

CLOSED, the corresponding value is 3, indicating that the connection has been closed or the connection cannot be opened.

When the WebSocket instantiated object is created, its readyState value must be set to CONNESTING.

extensionsThe initial value of the attribute must be an empty string. When the WebSocket connection is established, its value can be changed according to the following rules.

Note: If the server selected the extension, the extension property returns the extension selected by the server. (Currently this field is always an empty field)

protocolAttribute initialization must be an empty string . When the WebSocket connection is established, its value can be changed according to the following rules.

Note: If the server specifies a subprotocol, the protocol attribute returns the subprotocol selected by the server.

close() The method must be executed according to the following steps:

(1) If this method passes the first parameter but it is not equal to 1000 or not within 3000 to 4999, throw InvalidAccessErrorException and terminate the following steps.

(2) If the second parameter is passed in, execute the following sub-steps:

① Let raw reason become the second parameter of the method Parameter

( is omitted after )

(3) Execute the first matching step below

①If the readyState attribute is CLOSING or CLOSED, nothing happens .

②If the WebSocket connection has not been established, terminate the establishment of the WebSocket connection and ## The property value of #readyState is set to CLOSING.

③If the closing handshake of

WebSocket has not started, start the closing handshake of WebSocket, and readyState is set to CLOSING. If the first parameter is passed, then WebSocketThe status code used to close the message must be the integer specified by the first parameter. If the second parameter is also declared, reason must be given after the status code of the shutdown message.

④In other cases, set the

readyState attribute value to CLOSING.

The above is the detailed content of Detailed introduction to WebSocket API HTML5 specification translation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template