@@ -795,6 +795,128 @@ the following list:
795
795
796
796
</div>
797
797
798
+
799
+
800
+ # The {{WebSocketStream}} interface # {#the-websocketstream-interface}
801
+
802
+ The Web IDL definition for the {{WebSocketStream}} class is given as follows:
803
+
804
+ <xmp class="idl">
805
+ dictionary WebSocketOpenInfo {
806
+ ReadableStream readable;
807
+ WritableStream writable;
808
+ DOMString extensions;
809
+ DOMString protocol;
810
+ };
811
+
812
+ dictionary WebSocketCloseInfo {
813
+ [Clamp] unsigned short code;
814
+ USVString reason = "";
815
+ };
816
+
817
+ dictionary WebSocketStreamOptions {
818
+ sequence<USVString> protocols;
819
+ AbortSignal signal;
820
+ };
821
+
822
+ [
823
+ Exposed=(Window,Worker)
824
+ ] interface WebSocketStream {
825
+ constructor(USVString url, optional WebSocketStreamOptions options);
826
+ readonly attribute USVString url;
827
+ readonly attribute Promise<WebSocketOpenInfo> opened;
828
+ readonly attribute Promise<WebSocketCloseInfo> closed;
829
+ undefined close(optional WebSocketCloseInfo closeInfo);
830
+ };
831
+ </xmp>
832
+
833
+ A {{WebSocketStream}} object has an associated <dfn>url</dfn> (a [=URL record=] ).
834
+
835
+ A {{WebSocketStream}} object has an associated <dfn>opened promise</dfn> .
836
+
837
+ A {{WebSocketStream}} object has an associated <dfn>closed promise</dfn> .
838
+
839
+ A {{WebSocketStream}} object has an associated <dfn>was ever connected flag</dfn> , which is
840
+ initially unset.
841
+
842
+ <dl class="domintro non-normative">
843
+ : <code> |socket| = new {{WebSocketStream/constructor(url, options)|WebSocketStream}} (|url| [, |options| ] </code>
844
+ :: Creates a new {{WebSocketStream}} object, immediately establishing the associating WebSocket
845
+ connection.
846
+
847
+ |url| is a string giving the <a dfn spec=url>URL</a> over which the connection is established.
848
+ Only "`ws`" or "`wss`" schemes are allowed; others will cause a "{{SyntaxError}} "
849
+ {{DOMException}} . URLs with [=fragments=] will also cause such an exception.
850
+
851
+ The |options| argument is an object whose properties can be set as follows:
852
+
853
+ : {{WebSocketStreamOptions/protocols}}
854
+ :: An array of strings. If it is omitted, it is equivalent to an empty array. Each string in the
855
+ array is a subprotocol name. The connection will only be established if the server reports that
856
+ it has selected one of these subprotocols. The subprotocol names have to match the requirements
857
+ for elements that comprise the value of \`<a http-header>`Sec-WebSocket-Protocol`</a> \` fields as
858
+ defined by The WebSocket protocol. [[!WSP]]
859
+
860
+ : {{WebSocketStreamOptions/signal}}
861
+ :: An {{AbortSignal}} that can be used to abort the handshake. After the handshake is complete the
862
+ signal does nothing.
863
+
864
+ : <code> |socket| . {{WebSocketStream/url}} </code>
865
+ :: Returns the <a lt="url">URL that was used</a> to establish the WebSocket connection.
866
+
867
+ : <code> |socket| . {{WebSocketStream/opened}} </code>
868
+ :: Returns a {{promise}} which resolves when the handshake successfully completes, or rejects if
869
+ the handshake fails. On success, it resolves to an object with the following properties:
870
+
871
+ : {{WebSocketOpenInfo/readable}}
872
+ :: A {{ReadableStream}} that can be used to read messages from the server. Each chunk read
873
+ corresponds to one message. Text messages will be read as strings; binary messages will be read
874
+ as ArrayBuffer objects.
875
+
876
+ The stream can be closed by calling {{ReadableStream/cancel()}} on
877
+ {{WebSocketOpenInfo/readable}} . If the argument passed to {{ReadableStream/cancel()}} is an
878
+ object with a {{WebSocketCloseInfo/code}} property and an <span class=allow-2119> optional</span> {{WebSocketCloseInfo/reason}}
879
+ property then {{WebSocketCloseInfo/code}} will be used as [=the WebSocket connection close
880
+ code=] and {{WebSocketCloseInfo/reason}} or the empty string will be used as [=the WebSocket
881
+ connection close reason=] .
882
+
883
+ If no messages are read, or if messages are read slower than they are sent, then backpressure
884
+ will be applied and eventually the server will stop sending new messages.
885
+
886
+ : {{WebSocketOpenInfo/writable}}
887
+ :: A {{WritableStream}} that can be used to send messages to the server. Each chunk written will
888
+ be converted to one messages. Strings will be sent as text messages; {{BufferSource}} chunks will
889
+ be sent as binary messages.
890
+
891
+ The WebSocket can be closed by calling {{WritableStream/close()}} on
892
+ {{WebSocketOpenInfo/writable}} .
893
+
894
+ The stream can also be closed by calling {{WritableStream/abort()}}
895
+ {{WebSocketOpenInfo/writable}} . If an argument is passed to {{WritableStream/abort()}} then it
896
+ can be used to specify [=the WebSocket connection close code=] and [=the WebSocket connection
897
+ close reason=] as with {{ReadableStream/cancel()}} above.
898
+
899
+ : {{WebSocketOpenInfo/extensions}}
900
+ :: The [=extensions in use=] for the connection.
901
+
902
+ : {{WebSocketOpenInfo/protocol}}
903
+ :: The [=subprotocol in use=] for the connection.
904
+
905
+ : <code> |socket| . {{WebSocketStream/closed}} </code>
906
+ :: A {{promise}} which resolves when the connection is closed. If the connection did not close
907
+ [=cleanly=] then the promise is rejected. When the connection closes [=cleanly=] the promise is
908
+ resolved with an object with properties {{WebSocketCloseInfo/code}} and
909
+ {{WebSocketCloseInfo/reason}} , giving [=the WebSocket connection close code=] and [=the WebSocket
910
+ connection close reason=] the were supplied by the server.
911
+
912
+ : <code> |socket| . {{WebSocketStream/close()}} </code>
913
+ :: Close the connection, optionally supplying an object with {{WebSocketCloseInfo/code}} and
914
+ {{WebSocketCloseInfo/reason}} properties to indicate [=the WebSocket connection close code=] and
915
+ [=the WebSocket connection close reason=] that will be sent to the remote server. If the handshake
916
+ is still in progress then it will be aborted and {{WebSocketCloseInfo/code}} and
917
+ {{WebSocketCloseInfo/reason}} will be ignored.
918
+ </dl>
919
+
798
920
<h2 id="acks" class="no-num">Acknowledgments</h2>
799
921
800
922
Until the creation of this standard in 2021, the text here was maintained in the <a
0 commit comments