commit | 969bc6c859b4bb4a7ab5d01009f73a0ec623340c | [log] [tgz] |
---|---|---|
author | Ömer Sinan Ağacan <omeragacan@gmail.com> | Sat Dec 09 10:47:38 2023 +0100 |
committer | GitHub <noreply@github.com> | Sat Dec 09 10:47:38 2023 +0100 |
tree | be29b66e75429c16fef822e480c77de8b4f647c4 | |
parent | df096a9718e41f6883e683f177376a0cfd1b604c [diff] |
Fix JS value to Dart conversion when receiving from a web socket (#298) `MessageEvent` is a `package:web` type and `data` field is a JS value of type `JSAny?`. The receiving end of the `sink` is here: https://github.com/dart-lang/sdk/blob/26107a319a7503deafee404e3462644a873e2920/pkg/vm_service/lib/src/vm_service.dart#L1795 This code currently does not expect to see JS objects, so passing a `JSAny?` to the sink breaks it. The fix should be in the generating end rather than the receiving end: `JSAny?` is supposed to be an unboxed value, not a boxed Dart value. In an ideal world we shouldn't be able to pass it as a Dart object. So we call `dartify` and convert it to a Dart object. This fixes DevTools when compiled to Wasm. Thanks to @eyebrowsoffire and @mkustermann for help with debugging. Co-authored-by: Kevin Moore <kevmoo@google.com>
The web_socket_channel
package provides StreamChannel
wrappers for WebSocket connections. It provides a cross-platform WebSocketChannel
API, a cross-platform implementation of that API that communicates over an underlying StreamChannel
, an implementation that wraps dart:io
's WebSocket
class, and a similar implementation that wraps dart:html
's.
It also provides constants for the WebSocket protocol‘s pre-defined status codes in the status.dart
library. It’s strongly recommended that users import this library with the prefix status
.
import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:web_socket_channel/status.dart' as status; main() async { final wsUrl = Uri.parse('ws://example.com'); final channel = WebSocketChannel.connect(wsUrl); await channel.ready; channel.stream.listen((message) { channel.sink.add('received!'); channel.sink.close(status.goingAway); }); }
WebSocketChannel
The WebSocketChannel
class's most important role is as the interface for WebSocket stream channels across all implementations and all platforms. In addition to the base StreamChannel
interface, it adds a protocol
getter that returns the negotiated protocol for the socket, as well as closeCode
and closeReason
getters that provide information about why the socket closed.
The channel‘s sink
property is also special. It returns a WebSocketSink
, which is just like a StreamSink
except that its close()
method supports optional closeCode
and closeReason
parameters. These parameters allow the caller to signal to the other socket exactly why they’re closing the connection.
WebSocketChannel
also works as a cross-platform implementation of the WebSocket protocol. The WebSocketChannel.connect
constructor connects to a listening server using the appropriate implementation for the platform. The WebSocketChannel()
constructor takes an underlying StreamChannel
over which it communicates using the WebSocket protocol. It also provides the static signKey()
method to make it easier to implement the initial WebSocket handshake. These are used in the shelf_web_socket
package to support WebSockets in a cross-platform way.