-
Notifications
You must be signed in to change notification settings - Fork 23k
Some more WebSocketStream fixes
#40789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
cf89af3
f3d5fba
070a148
af51602
660f72c
88ee167
f4453d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,7 +29,8 @@ new WebSocketStream(url, options) | |||||||
| - `protocols` {{optional_inline}} | ||||||||
| - : A single string or an array of strings representing the sub-protocol(s) that the client would like to use, for example `"amqp"` or `"mqtt"`. Subprotocols may be selected from the [IANA WebSocket Subprotocol Name Registry](https://www.iana.org/assignments/websocket/websocket.xml#subprotocol-name) or may be custom names jointly understood by the client and the server. A single server can implement multiple WebSocket sub-protocols, and handle different types of interactions depending on the specified value. If it is omitted, an empty array is used by default. If `protocols` is included, the connection will only be established if the server reports that it has selected one of these sub-protocols. | ||||||||
| - `signal` {{optional_inline}} | ||||||||
| - : An {{domxref("AbortSignal")}} belonging to an {{domxref("AbortController")}} that you want to use to close the WebSocket connection. | ||||||||
| - : An {{domxref("AbortSignal")}} belonging to an {{domxref("AbortController")}}, which can be used to abort the *handshake*. This is specifically intended to make it easy to implement connection timeouts. As such, it does nothing after the connection is established. | ||||||||
|
|
||||||||
|
|
||||||||
| ### Exceptions | ||||||||
|
|
||||||||
|
|
@@ -47,26 +48,28 @@ const wss = new WebSocketStream("wss://example.com/wss"); | |||||||
| A more advanced example could also include an options object containing custom protocols and/or an {{domxref("AbortSignal")}}: | ||||||||
tomayac marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
|
|
||||||||
| ```js | ||||||||
| const controller = new AbortController(); | ||||||||
| const queueWSS = new WebSocketStream("wss://example.com/queue", { | ||||||||
| protocols: ["amqp", "mqtt"], | ||||||||
| signal: controller.signal, | ||||||||
| signal: AbortSignal.timeout(5000), | ||||||||
| }); | ||||||||
| ``` | ||||||||
|
|
||||||||
| At a later time, {{domxref("AbortController.abort()")}} can be called when required to close the connection: | ||||||||
| and the connect will timeout after 5 seconds. | ||||||||
|
|
||||||||
tomayac marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| ```js | ||||||||
| controller.abort(); | ||||||||
| ``` | ||||||||
| If you're connecting to localhost, it's likely to succeed or fail almost instantly, so this will have no effect. | ||||||||
|
|
||||||||
| Alternatively, you can use the {{domxref("WebSocketStream.close()")}} method to close a connection, however this is mainly needed if you wish to specify a custom code and/or reason for the server to report. | ||||||||
| You can use the {{domxref("WebSocketStream.close()")}} method to close a connection. | ||||||||
|
|
||||||||
| `WritableStream.close()` and `WritableStreamDefaultWriter.close()` from the `writable` of the fulfilled `opened` `Promise` also closes the connection. | ||||||||
|
|
||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd lean towards omitting this, as it looks a bit random after this update.
Suggested change
I suppose the argument for keeping it is to forestall confusion about using signal to close the connection. If we wanted to do that, we ought to frame it properly: "Note that you can't use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it makes sense to keep it, and to add something along the lines of what you wrote. Can you help author that?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's going to be much easier to do that as a follow up then, so I'll merge this as it is. |
||||||||
| See [Using WebSocketStream to write a client](/en-US/docs/Web/API/WebSockets_API/Using_WebSocketStream) for a complete example with full explanation. | ||||||||
tomayac marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
|
||||||||
| ## Specifications | ||||||||
|
|
||||||||
| Not currently a part of any specification. See https://github.com/whatwg/websockets/pull/48 for standardization progress. | ||||||||
| Not currently a part of any specification. See https://github.com/whatwg/websockets/pull/48 for standardization progress. | ||||||||
|
|
||||||||
| [WebSocketStream API design](https://docs.google.com/document/d/1La1ehXw76HP6n1uUeks-WJGFgAnpX2tCjKts7QFJ57Y/edit?pli=1&tab=t.0). | ||||||||
|
|
||||||||
|
|
||||||||
| ## Browser compatibility | ||||||||
|
|
||||||||
|
|
||||||||
Uh oh!
There was an error while loading. Please reload this page.