1. Introduction
To allow Web apps to maintain two-way communication with server processes, the WebSocket interface is described here .
2. Conformance requirements (abbreviated)
3. Terminology (abbreviated)
4.WebSocket Interface
(Code slightly)
The WebSocket (URL, protocols) constructor can pass in a 1-2 parameter. The first host specifies the URL to connect to . The second argument, if declared, can be a string or an array of strings. If it is a string, it is equivalent to an array containing only that string. If the second argument is omitted, it is equivalent to an empty array "that is treated as a string array regardless of what is passed in the second argument." The string in each of the arrays is a sub-protocol name. A connection can be established only if the server declares that it has selected one of the sub-protocols. All sub-protocol names must meet the following requirements: These strings will contain the content ① of the sec-websocket-protocol of the WebSocket Protocol declaration Header .
When the WebSocket () constructor is called, the user agent must perform the following steps:
(1) from parameters parse out url syntaxerror exception and terminate the steps below.
(2) If secure is empty, but the original entry script has a schema build itself is a security protocol, such as HTTPS, then throw Securityerror exception. (If secure is false but the origin of the entry script have a scheme component that's itself a secure PROTOC OL, e.g. HTTPS, then throw a Securityerror exception.)
(3) If the use port in the parameter is set to forbidden by the user agent, it will throwSecurityerrorexception. Port theand the443should not be banned from access, includingSecureis afalsethe case443the port cannot be blocked from Access,Secureis atruethe time thePort cannot be forbidden to access "Here's to say,WebSocketThere are two kinds of forms,wsis not with the security protocol, and is used by default thePorts,WSSis a security protocol, which is used by default443port, but even if the protocol you are using does not require a corresponding port, you cannot disable access443and the thethese two ports. "
(4) If the protocols parameter is not declared , let it be an empty array. Otherwise, if the protocols parameter is a string, let it become an array containing the string.
(5) If any of the variables in the protocols parameter are declared to be above or not satisfied with the ① requirement, the syntaxerror exception is thrown and the following steps are terminated.
(6) Let Origin is the ASCII serialization of the origin of the entry script, converted to ASCII lowercase.
(7) Return a new WebSocket object and continue with the following steps in the background program.
(8) Create an includedhost, port, resource name, and secureas wellProtocolslist, and an empty extension(extensions)List andOriginof theWebSocketconnection. The head wants to send the rightCookiesmust useCookiesheader, where the value is stored through the user 'sCookiesvalues, andURLcalculated. For these purposes, this is not aHTTPof the AgreementAPI.
During theWebSocket Setup connection algorithm execution, when the user agent verifies the response of the server, if the status code received from the server is not 101, the user agent must terminate this WebSocket connection.
warning! This uses the http websocket server established on one path, an open http Redirect to another path. Suddenly, any specified websocket url The script can enter this service and communicate with other hosts on the Internet, and the instant script checks url
Note: If the WebSocket connection algorithm fails to execute, the WebSocket connection failure algorithm is triggered, and the algorithm executes the shutdown WebSocket The connected algorithm, which closes the WebSocket connection, triggers the close event, and executes the steps as described below.
This constructor must be visible when the global object of the script is a Window object or an object instantiated through the Workerutils interface.
The URL property must be parsed from the URL passed for the constructor .
The ReadyState property represents the state of the connection, and 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 that the WebSocket Connection has been established and can communicate;
CLOSING, the corresponding value is 2, indicating that the connection is closing the handshake, or that 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 an WebSocket Instantiation object is created, its readyState value must be set to connesting.
the Extensions property initial value 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 chooses the extension, theextension property returns the extension of the server selection. ( currently this field is an empty field )
the Protocol property 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 sub-protocol, theprotocol property returns the sub-protocol selected by the server.
The close () method must follow these steps:
(1) If the method passes the first parameter but not equal to or not within the 4999 , throws Invalidaccesserror The exception and terminates the following steps.
(2) If the second parameter is passed in, the following sub-steps are performed:
① make Raw reason the second parameter of a method
②( omitted later )
(3) Perform the first match to the following steps
① if the readyState property is CLOSING or CLOSED, nothing happens.
② if the WebSocket Connection has not been established, terminates The establishment of the WebSocket connection and will readyState The property value is set to CLOSING.
③ if WebSocket 's close handshake hasn't started yet, start WebSocket 's off handshake and set readyState to CLOSING . If the first parameter is passed, then The status code used for the WebSocket shutdown 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 closing message .
④ other cases, set the readyState property value to CLOSING.
WebSocket API HTML5 Specification translation (partial)