Skip to content

Commit

Permalink
Avoid browser warnings when closing unestablished WebSocket (#799)
Browse files Browse the repository at this point in the history
* Avoid browser warnings when closing unestablished WebSocket

* PR link
  • Loading branch information
tomaka authored Jun 23, 2023
1 parent a83318a commit e93b7e5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixed

- Fix not absorbing the JavaScript exception triggered by the browser when connecting to a `ws://` node when smoldot is embedded in a web page served over `https://`. ([#795](https://github.com/smol-dot/smoldot/pull/795), [#800](https://github.com/smol-dot/smoldot/pull/800))
- Smoldot no longer calls `close()` on WebSockets that aren't fully established yet (even though it is legal to do so according to the WHATWG specification) in order to avoid browsers printing warnings in the console when you do so. ([#799](https://github.com/smol-dot/smoldot/pull/799))

## 1.0.10 - 2023-06-19

Expand Down
12 changes: 11 additions & 1 deletion wasm-node/javascript/src/no-auto-bytecode-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,17 @@ function connect(config: ConnectionConfig): Connection {
connection.onclose = null;
connection.onmessage = null;
connection.onerror = null;
connection.close();

// According to the WebSocket specification, calling `close()` when a WebSocket
// isn't fully opened yet is completely legal and seemingly a normal thing to
// do (see <https://websockets.spec.whatwg.org/#dom-websocket-close>).
// Unfortunately, browsers print a warning in the console if you do that. To
// avoid these warnings, we only call `close()` if the connection is fully
// opened. According to <https://websockets.spec.whatwg.org/#garbage-collection>,
// removing all the event listeners will cause the WebSocket to be garbage
// collected, which should have the same effect as `close()`.
if (connection.readyState == WebSocket.OPEN)
connection.close();
}

connection = null;
Expand Down

0 comments on commit e93b7e5

Please sign in to comment.