### Future get ready Source: https://pub.dev/documentation/web_socket_channel/latest/adapter_web_socket_channel/AdapterWebSocketChannel/ready.html The ready property returns a Future that completes when the WebSocket connection is established. It is used to wait for the connection before sending data. ```APIDOC ## Future get ready ### Description A future that will complete when the WebSocket connection has been established. This future must be complete before data can be sent using WebSocketChannel.sink. If a connection could not be established, this future will complete with an error. ### Method GET ### Endpoint ready ### Parameters None ### Request Example None ### Response #### Success Response (200) - **Future** - A future that completes when the connection is established. #### Response Example ```dart final channel = WebSocketChannel.connect(Uri.parse('ws://example.com')); try { await channel.ready; } on SocketException catch (e) { // Handle the exception. } on WebSocketChannelException catch (e) { // Handle the exception. } // If `ready` completes without an error then the channel is ready to // send data. channel.sink.add('Hello World'); ``` ``` -------------------------------- ### HtmlWebSocketChannel constructor implementation Source: https://pub.dev/documentation/web_socket_channel/latest/html/HtmlWebSocketChannel/HtmlWebSocketChannel.html This constructor initializes the channel by wrapping the provided WebSocket. It handles the ready state: if open, it completes the ready completer and starts listening; if closing or closed, it completes with an error. It also sets up error and close handlers to propagate errors and close the channel. ```dart HtmlWebSocketChannel(Object /*WebSocket*/ webSocket) : innerWebSocket = webSocket as WebSocket { _readyCompleter = Completer(); if (innerWebSocket.readyState == WebSocket.OPEN) { _readyCompleter.complete(); _listen(); } else { if (innerWebSocket.readyState == WebSocket.CLOSING || innerWebSocket.readyState == WebSocket.CLOSED) { _readyCompleter.completeError(WebSocketChannelException( 'WebSocket state error: ${innerWebSocket.readyState}')); } // The socket API guarantees that only a single open event will be // emitted. innerWebSocket.onOpen.first.then((_) { _readyCompleter.complete(); _listen(); }); } // The socket API guarantees that only a single error event will be emitted, // and that once it is no open or message events will be emitted. innerWebSocket.onError.first.then((_) { // Unfortunately, the underlying WebSocket API doesn't expose any // specific information about the error itself. final error = WebSocketChannelException('WebSocket connection failed.'); if (!_readyCompleter.isCompleted) { _readyCompleter.completeError(error); } _controller.local.sink.addError(error); _controller.local.sink.close(); }); innerWebSocket.onMessage.listen(_innerListen); // The socket API guarantees that only a single error event will be emitted, // and that once it is no other events will be emitted. innerWebSocket.onClose.first.then((event) { _closeCode = event.code; _closeReason = event.reason; _controller.local.sink.close(); }); } ``` -------------------------------- ### signKey implementation Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel/signKey.html The implementation of the signKey method, which encodes the key with the WebSocket GUID using SHA-1 and base64. ```dart static String signKey(String key) // We use [codeUnits] here rather than UTF-8-decoding the string because // [key] is expected to be base64 encoded, and so will be pure ASCII. => convert.base64 .encode(sha1.convert((key + _webSocketGUID).codeUnits).bytes); ``` -------------------------------- ### protocol (getter) Source: https://pub.dev/documentation/web_socket_channel/latest/adapter_web_socket_channel/AdapterWebSocketChannel/protocol.html Gets the subprotocol selected by the server. Initially null, it is set after the WebSocket connection is established if a subprotocol is negotiated. ```APIDOC ## protocol (getter) ### Description Gets the subprotocol selected by the server. For a client socket, this is initially `null`. After the WebSocket connection is established, the value is set to the subprotocol selected by the server. If no subprotocol is negotiated, the value remains `null`. ### Method GET (property access) ### Endpoint Property: `protocol` ### Return Value - **String?** - The subprotocol selected by the server, or `null` if not negotiated. ### Implementation ```dart @override String? get protocol => _protocol; ``` ``` -------------------------------- ### Close a WebSocket channel with status.goingAway Source: https://pub.dev/documentation/web_socket_channel/latest/status Import the status library with a prefix and use the goingAway constant when closing a WebSocket channel. The example connects to a local WebSocket server and closes the channel with the status code. ```dart import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:web_socket_channel/status.dart' as status; void main() async { var channel = WebSocketChannel.connect(Uri.parse('ws://localhost:1234')); // ... channel.close(status.goingAway); } ``` -------------------------------- ### closeReason Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel/closeReason.html Gets the close reason set when the WebSocket connection is closed. Returns null before the connection has been closed. ```APIDOC ## closeReason ### Description Gets the close reason set when the WebSocket connection is closed. Returns `null` before the connection has been closed. ### Method Getter ### Return Value - **String?** - The close reason, or `null` if the connection has not been closed yet. ### Implementation ```dart String? get closeReason; ``` ``` -------------------------------- ### closeCode getter Source: https://pub.dev/documentation/web_socket_channel/latest/html/HtmlWebSocketChannel/closeCode.html Gets the close code set when the WebSocket connection is closed. Returns null before the connection is closed. ```APIDOC ## closeCode getter ### Description Gets the close code set when the WebSocket connection is closed. Returns null before the connection is closed. ### Method GET ### Endpoint closeCode ### Parameters None ### Response #### Success Response (200) - **closeCode** (int?) - The close code, or null if the connection is not yet closed. #### Response Example ```json { "closeCode": 1000 } ``` ``` -------------------------------- ### Get bufferedAmount from innerWebSocket Source: https://pub.dev/documentation/web_socket_channel/latest/html/HtmlWebSocketChannel/bufferedAmount.html Getter that returns the number of bytes queued but not yet transmitted to the network. It delegates to the inner WebSocket's bufferedAmount property. ```dart int? get bufferedAmount => innerWebSocket.bufferedAmount; ``` -------------------------------- ### Stream get stream Source: https://pub.dev/documentation/web_socket_channel/latest/adapter_web_socket_channel/AdapterWebSocketChannel/stream.html Returns the single-subscription stream that emits values from the other endpoint. This getter overrides the base implementation and provides access to the underlying foreign controller's stream. ```APIDOC ## Stream get stream ### Description Returns the single-subscription stream that emits values from the other endpoint. ### Method Getter ### Endpoint N/A (SDK property) ### Parameters None ### Return Value - **Stream** - The single-subscription stream emitting values from the other endpoint. ### Implementation ```dart @override Stream get stream => _controller.foreign.stream; ``` ``` -------------------------------- ### Future get done Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketSink/done.html Returns a future that completes when the StreamSink is finished. If the sink fails with an error, the future completes with that error. Otherwise, it completes when all events have been processed and the sink is closed, or when the sink is stopped from handling more events. ```APIDOC ## Future get done ### Description Returns a future which is completed when the StreamSink is finished. If the `StreamSink` fails with an error, perhaps in response to adding events using `add`, `addError` or `close`, the `done` future will complete with that error. Otherwise, the returned future will complete when either all events have been processed and the sink has been closed, or the sink has otherwise been stopped from handling more events (for example by canceling a stream subscription). ### Method Getter ### Return Value `Future` - A future that completes when the sink is finished. ### Implementation ```dart @override Future get done => _sink.done; ``` ``` -------------------------------- ### Override WebSocketSink get sink Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel/sink.html Override this getter to provide a WebSocketSink for sending values to the other endpoint. The sink supports additional arguments to WebSocketSink.close that provide the remote endpoint reasons for closing the connection. ```dart @override WebSocketSink get sink; ``` -------------------------------- ### WebSocketChannel.connect implementation Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel/connect.html This is the implementation of the connect static method. It delegates to AdapterWebSocketChannel.connect, passing the uri and optional protocols. The method returns a WebSocketChannel synchronously, but the connection is established asynchronously; the ready future completes after the connection is established or with an error if creation fails. ```dart static WebSocketChannel connect(Uri uri, {Iterable? protocols}) => AdapterWebSocketChannel.connect(uri, protocols: protocols); ``` -------------------------------- ### HtmlWebSocketChannel.connect constructor implementation Source: https://pub.dev/documentation/web_socket_channel/latest/html/HtmlWebSocketChannel/HtmlWebSocketChannel.connect.html Constructor implementation for HtmlWebSocketChannel.connect. It creates a WebSocket from the given URL and protocols, and sets the binaryType. The binaryType defaults to BinaryType.list, which delivers binary messages as Uint8Lists; if set to BinaryType.blob, they are delivered as Blobs. ```dart HtmlWebSocketChannel.connect(Object url, {Iterable? protocols, BinaryType? binaryType}) : this( WebSocket( url.toString(), protocols?.map((e) => e.toJS).toList().toJS ?? JSArray(), )..binaryType = (binaryType ?? BinaryType.list).value, ); ``` -------------------------------- ### IOWebSocketChannel.connect(Object url, {Iterable? protocols, Map? headers, Duration? pingInterval, Duration? connectTimeout, HttpClient? customClient}) Source: https://pub.dev/documentation/web_socket_channel/latest/io/IOWebSocketChannel-class.html Creates a new WebSocket connection to the specified URL with optional protocols, headers, ping interval, connect timeout, and custom HTTP client. ```APIDOC ## IOWebSocketChannel.connect(Object url, {Iterable? protocols, Map? headers, Duration? pingInterval, Duration? connectTimeout, HttpClient? customClient}) ### Description Creates a new WebSocket connection to the given URL. This is a factory constructor. ### Constructor `IOWebSocketChannel.connect(Object url, {Iterable? protocols, Map? headers, Duration? pingInterval, Duration? connectTimeout, HttpClient? customClient})` ### Parameters - **url** (Object) - Required - The URL to connect to. - **protocols** (Iterable?) - Optional - Subprotocols to request. - **headers** (Map?) - Optional - Additional headers to send. - **pingInterval** (Duration?) - Optional - Interval for sending ping frames. - **connectTimeout** (Duration?) - Optional - Timeout for the connection attempt. - **customClient** (HttpClient?) - Optional - Custom HTTP client to use. ``` -------------------------------- ### Methods Source: https://pub.dev/documentation/web_socket_channel/latest/adapter_web_socket_channel/AdapterWebSocketChannel-class.html Methods available on AdapterWebSocketChannel for stream and sink manipulation. ```APIDOC ## Methods ### cast() Returns a copy of this with the generic type coerced to `S`. ### changeSink(StreamSink change(StreamSink)) Returns a copy of this with sink replaced by `change`'s return value. ### changeStream(Stream change(Stream)) Returns a copy of this with stream replaced by `change`'s return value. ### pipe(StreamChannel other) Connects this to `other`, so that any values emitted by either are sent directly to the other. ### transform(StreamChannelTransformer transformer) Transforms this using `transformer`. ### transformSink(StreamSinkTransformer transformer) Transforms only the sink component of this using `transformer`. ### transformStream(StreamTransformer transformer) Transforms only the stream component of this using `transformer`. ``` -------------------------------- ### IOWebSocketChannel.connect implementation Source: https://pub.dev/documentation/web_socket_channel/latest/io/IOWebSocketChannel/IOWebSocketChannel.connect.html The implementation of the IOWebSocketChannel.connect factory constructor. It creates a WebSocket connection, sets the ping interval, applies a timeout if specified, and returns an IOWebSocketChannel wrapping the future. ```dart factory IOWebSocketChannel.connect( Object url, { Iterable? protocols, Map? headers, Duration? pingInterval, Duration? connectTimeout, HttpClient? customClient, }) { var webSocketFuture = WebSocket.connect( url.toString(), headers: headers, protocols: protocols, customClient: customClient, ).then((webSocket) => webSocket..pingInterval = pingInterval); if (connectTimeout != null) { webSocketFuture = webSocketFuture.timeout(connectTimeout); } return IOWebSocketChannel(webSocketFuture); } ``` -------------------------------- ### AdapterWebSocketChannel.connect factory constructor implementation Source: https://pub.dev/documentation/web_socket_channel/latest/adapter_web_socket_channel/AdapterWebSocketChannel/AdapterWebSocketChannel.connect.html The implementation of the AdapterWebSocketChannel.connect factory constructor. It creates an AdapterWebSocketChannel using WebSocket.connect with the given URL and optional protocols. The ready future completes after connection or with an error. ```dart factory AdapterWebSocketChannel.connect(Uri url, {Iterable? protocols}) => AdapterWebSocketChannel(WebSocket.connect(url, protocols: protocols)); ``` -------------------------------- ### WebSocketChannel.connect Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel/connect.html Documentation for the static connect method of WebSocketChannel. ```APIDOC ## WebSocketChannel.connect ### Description Creates a new WebSocket connection to the given URI and returns a WebSocketChannel for communication. The connection is established asynchronously; the `ready` future completes when the connection is established or with an error if connection fails. ### Method static WebSocketChannel connect(Uri uri, {Iterable? protocols}) ### Parameters - **uri** (Uri) - Required - The URI to connect to. - **protocols** (Iterable?) - Optional - The same as `WebSocket.connect` protocols parameter. ### Returns - **WebSocketChannel** - A channel for communicating over the WebSocket connection. The connection is not established synchronously; use the `ready` future to know when the connection is ready. ### Implementation ```dart static WebSocketChannel connect(Uri uri, {Iterable? protocols}) => AdapterWebSocketChannel.connect(uri, protocols: protocols); ``` ``` -------------------------------- ### ready Source: https://pub.dev/documentation/web_socket_channel/latest/html/HtmlWebSocketChannel/ready.html A future that completes when the WebSocket connection has been established. Must be awaited before sending data. ```APIDOC ## ready ### Description A future that will complete when the WebSocket connection has been established. This future must be complete before data can be sent using WebSocketChannel.sink. If a connection could not be established (e.g. because of a network issue), then this future will complete with an error. ### Method GET (property getter) ### Endpoint N/A (property access) ### Parameters None ### Request Example N/A ### Response #### Success Response (200) - **Future** - Completes when the connection is established. #### Response Example ```dart final channel = WebSocketChannel.connect(Uri.parse('ws://example.com')); try { await channel.ready; } on SocketException catch (e) { // Handle the exception. } on WebSocketChannelException catch (e) { // Handle the exception. } // If `ready` completes without an error then the channel is ready to // send data. channel.sink.add('Hello World'); ``` ### Implementation ```dart @override Future get ready => _readyCompleter.future; ``` ``` -------------------------------- ### AdapterWebSocketChannel.connect(Uri url, {Iterable? protocols}) Source: https://pub.dev/documentation/web_socket_channel/latest/adapter_web_socket_channel/AdapterWebSocketChannel-class.html Creates a new WebSocket connection to the given URL with optional protocols. ```APIDOC ## AdapterWebSocketChannel.connect(Uri url, {Iterable? protocols}) ### Description Creates a new WebSocket connection to the specified URL, optionally specifying subprotocols. ### Factory Constructor AdapterWebSocketChannel.connect(Uri url, {Iterable? protocols}) ### Parameters - **url** (Uri) - Required - The URL to connect to. - **protocols** (Iterable?) - Optional - The subprotocols to request. ``` -------------------------------- ### Connect to a WebSocket and handle messages Source: https://pub.dev/documentation/web_socket_channel/latest/index.html Connect to a WebSocket server using WebSocketChannel.connect and listen for incoming messages, responding with a status code upon closing. ```dart 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); }); } ``` -------------------------------- ### Using WebSocketChannel.ready to await connection Source: https://pub.dev/documentation/web_socket_channel/latest/adapter_web_socket_channel/AdapterWebSocketChannel/ready.html Shows how to await the `ready` future before sending data. Catches `SocketException` and `WebSocketChannelException` if the connection fails. After `ready` completes without error, data can be sent via `channel.sink.add('Hello World')`. ```dart final channel = WebSocketChannel.connect(Uri.parse('ws://example.com')); try { await channel.ready; } on SocketException catch (e) { // Handle the exception. } on WebSocketChannelException catch (e) { // Handle the exception. } // If `ready` completes without an error then the channel is ready to // send data. channel.sink.add('Hello World'); ``` -------------------------------- ### Implementation of ready getter Source: https://pub.dev/documentation/web_socket_channel/latest/adapter_web_socket_channel/AdapterWebSocketChannel/ready.html The implementation of the `ready` getter returns `_readyCompleter.future`. This is the internal implementation shown in the documentation. ```dart @override Future get ready => _readyCompleter.future; ``` -------------------------------- ### WebSocketChannel.connect(Uri uri, {Iterable? protocols}) → WebSocketChannel Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel-class.html Creates a new WebSocket connection to the specified URI, optionally listing WebSocket subprotocols. ```APIDOC ## WebSocketChannel.connect(Uri uri, {Iterable? protocols}) ### Description Creates a new WebSocket connection to the given URI, with optional protocol support. ### Method Static Method ### Signature `WebSocketChannel.connect(Uri uri, {Iterable? protocols}) → WebSocketChannel` ### Parameters - **uri** (Uri) - Required - The WebSocket endpoint URI. - **protocols** (Iterable?) - Optional - A list of WebSocket subprotocols for the connection. ### Returns - **WebSocketChannel** - The WebSocket channel for communication. ``` -------------------------------- ### HtmlWebSocketChannel.new Source: https://pub.dev/documentation/web_socket_channel/latest/html/HtmlWebSocketChannel/HtmlWebSocketChannel.html Creates a channel wrapping a dart:html WebSocket instance or a package:web WebSocket instance. The constructor handles connection readiness and error/close events internally. ```APIDOC ## HtmlWebSocketChannel constructor ### Description Creates a WebSocket channel wrapping the given WebSocket object. The channel internally manages the connection lifecycle, throwing an error if the socket is in a closing or closed state, and listening for open, error, message, and close events. ### Constructor `HtmlWebSocketChannel(Object webSocket)` ### Parameters - **webSocket** (Object) - Required - The WebSocket instance to wrap. Should be either a dart:html `WebSocket` or a package:web `WebSocket`. ### Behavior - If the WebSocket is already open, the channel becomes ready and starts listening. - If the WebSocket is closing or closed, the channel emits a `WebSocketChannelException` with the state error. - If the WebSocket connects later, the channel becomes ready once the open event fires. - If a connection error occurs, a `WebSocketChannelException` is emitted and the channel is closed. - On close, the channel's sink is closed with the close code and reason from the WebSocket. ### Implementation ```dart HtmlWebSocketChannel(Object /*WebSocket*/ webSocket) : innerWebSocket = webSocket as WebSocket { _readyCompleter = Completer(); if (innerWebSocket.readyState == WebSocket.OPEN) { _readyCompleter.complete(); _listen(); } else { if (innerWebSocket.readyState == WebSocket.CLOSING || innerWebSocket.readyState == WebSocket.CLOSED) { _readyCompleter.completeError(WebSocketChannelException( 'WebSocket state error: ${innerWebSocket.readyState}')); } innerWebSocket.onOpen.first.then((_) { _readyCompleter.complete(); _listen(); }); } innerWebSocket.onError.first.then((_) { final error = WebSocketChannelException('WebSocket connection failed.'); if (!_readyCompleter.isCompleted) { _readyCompleter.completeError(error); } _controller.local.sink.addError(error); _controller.local.sink.close(); }); innerWebSocket.onMessage.listen(_innerListen); innerWebSocket.onClose.first.then((event) { _closeCode = event.code; _closeReason = event.reason; _controller.local.sink.close(); }); } ``` ``` -------------------------------- ### AdapterWebSocketChannel constructor implementation Source: https://pub.dev/documentation/web_socket_channel/latest/adapter_web_socket_channel/AdapterWebSocketChannel/AdapterWebSocketChannel.html The constructor initializes a WebSocketChannel from a WebSocket or Future. It listens to WebSocket events (text, binary, close) and forwards them to the local sink, and listens to the local stream to send data or close the WebSocket. Errors are handled with TimeoutException or WebSocketChannelException. ```dart AdapterWebSocketChannel(FutureOr webSocket) { Future webSocketFuture; if (webSocket is WebSocket) { webSocketFuture = Future.value(webSocket); } else { webSocketFuture = webSocket; } webSocketFuture.then((webSocket) { webSocket.events.listen((event) { switch (event) { case TextDataReceived(text: final text): _controller.local.sink.add(text); case BinaryDataReceived(data: final data): _controller.local.sink.add(data); case CloseReceived(code: final code, reason: final reason): _closeCode = code; _closeReason = reason; _controller.local.sink.close(); } }); _controller.local.stream.listen((obj) { try { switch (obj) { case final String s: webSocket.sendText(s); case final Uint8List b: webSocket.sendBytes(b); case final List b: webSocket.sendBytes(Uint8List.fromList(b)); default: throw UnsupportedError('Cannot send ${obj.runtimeType}'); } } on WebSocketConnectionClosed { // There is nowhere to surface this error; `_controller.local.sink` // has already been closed. } }, onDone: () async { try { await webSocket.close(_localCloseCode, _localCloseReason); } on WebSocketConnectionClosed { // It is not an error to close an already-closed `WebSocketChannel`. } }); _protocol = webSocket.protocol; _readyCompleter.complete(); }, onError: (Object e) { Exception error; if (e is TimeoutException) { // Required for backwards compatibility with `IOWebSocketChannel`. error = e; } else { error = WebSocketChannelException.from(e); } _readyCompleter.completeError(error); _controller.local.sink.addError(error); _controller.local.sink.close(); }); } ``` -------------------------------- ### AdapterWebSocketChannel(FutureOr webSocket) Source: https://pub.dev/documentation/web_socket_channel/latest/adapter_web_socket_channel/AdapterWebSocketChannel-class.html Creates an AdapterWebSocketChannel from an existing WebSocket instance. ```APIDOC ## AdapterWebSocketChannel(FutureOr webSocket) ### Description Creates an AdapterWebSocketChannel from an existing WebSocket instance. ### Constructor AdapterWebSocketChannel(FutureOr webSocket) ### Parameters - **webSocket** (FutureOr) - Required - The WebSocket instance to wrap. ``` -------------------------------- ### signKey(String key) Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel/signKey.html Signs a `Sec-WebSocket-Key` header sent by a WebSocket client as part of the initial handshake. The return value should be sent back to the client in a `Sec-WebSocket-Accept` header. ```APIDOC ## signKey(String key) ### Description Signs a `Sec-WebSocket-Key` header sent by a WebSocket client as part of the initial handshake. The return value should be sent back to the client in a `Sec-WebSocket-Accept` header. ### Method static String signKey(String key) ### Parameters - **key** (String) - Required - The `Sec-WebSocket-Key` header value from the client. ### Return Value - **String** - The value to be sent in the `Sec-WebSocket-Accept` header. ### Implementation ```dart static String signKey(String key) // We use [codeUnits] here rather than UTF-8-decoding the string because // [key] is expected to be base64 encoded, and so will be pure ASCII. => convert.base64 .encode(sha1.convert((key + _webSocketGUID).codeUnits).bytes); ``` ``` -------------------------------- ### IOWebSocketChannel constructor implementation Source: https://pub.dev/documentation/web_socket_channel/latest/io/IOWebSocketChannel/IOWebSocketChannel.html Constructor implementation that wraps a WebSocket or Future into an IOWebSocketChannel. It converts the WebSocket to an IOWebSocket, handling both synchronous and asynchronous cases. ```dart IOWebSocketChannel(FutureOr webSocket) : super(webSocket is Future ? webSocket.then(IOWebSocket.fromWebSocket) as FutureOr : IOWebSocket.fromWebSocket(webSocket)); ``` -------------------------------- ### HtmlWebSocketChannel.connect Source: https://pub.dev/documentation/web_socket_channel/latest/html/HtmlWebSocketChannel/HtmlWebSocketChannel.connect.html Creates a new WebSocket connection to the specified URL, returning a channel for communication. The url can be a String or Uri, protocols are optional, and binaryType controls binary message delivery. ```APIDOC ## HtmlWebSocketChannel.connect ### Description Creates a new WebSocket connection to the given `url` and returns a channel for communication over the resulting socket. The `url` may be a String or a Uri. The `protocols` parameter is the same as for `WebSocket.new`. The `binaryType` parameter controls the type used for binary messages, defaulting to `BinaryType.list` (delivered as `Uint8List`), or `BinaryType.blob` (delivered as `Blob`). ### Constructor `HtmlWebSocketChannel.connect(Object url, {Iterable? protocols, BinaryType? binaryType})` ### Parameters - **url** (Object) - Required - The URL to connect to, either a String or a Uri. - **protocols** (Iterable?) - Optional - The subprotocols to use, same as for `WebSocket.new`. - **binaryType** (BinaryType?) - Optional - Controls the type for binary messages. Defaults to `BinaryType.list`. ### Implementation dart HtmlWebSocketChannel.connect(Object url, {Iterable? protocols, BinaryType? binaryType}) : this( WebSocket( url.toString(), protocols?.map((e) => e.toJS).toList().toJS ?? JSArray(), )..binaryType = (binaryType ?? BinaryType.list).value, ); ``` -------------------------------- ### IOWebSocketChannel(FutureOr webSocket) Source: https://pub.dev/documentation/web_socket_channel/latest/io/IOWebSocketChannel-class.html Creates a channel wrapping the given WebSocket. ```APIDOC ## IOWebSocketChannel(FutureOr webSocket) ### Description Creates a channel wrapping the provided `webSocket`. ### Constructor `IOWebSocketChannel(FutureOr webSocket)` ### Parameters - **webSocket** (FutureOr) - Required - The WebSocket to wrap. ``` -------------------------------- ### status.noStatusReceived Source: https://pub.dev/documentation/web_socket_channel/latest/status No status code was present. ```APIDOC ## status.noStatusReceived ### Description No status code was present. ### Type const int ``` -------------------------------- ### WebSocketChannel.ready getter declaration Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel/ready.html Declaration of the `ready` getter that returns a Future which completes when the WebSocket connection is established. ```dart Future get ready; ``` -------------------------------- ### String toString() Source: https://pub.dev/documentation/web_socket_channel/latest/html/BinaryType/toString.html Returns a string representation of this object. The implementation returns the name property. ```APIDOC ## String toString() ### Description Returns a string representation of this object. The implementation returns the `name` property. ### Method `toString` ### Signature `String toString()` ### Implementation ```dart @override String toString() => name; ``` ### Returns - **String** - The string representation of the object, which is the `name` property. ``` -------------------------------- ### IOWebSocketChannel Source: https://pub.dev/documentation/web_socket_channel/latest/io Documentation for the IOWebSocketChannel class, which provides a WebSocket channel using dart:io. ```APIDOC ## IOWebSocketChannel ### Description A WebSocketChannel that communicates using a `dart:io` WebSocket. ### Class IOWebSocketChannel ### Usage This class is used to create a WebSocket channel that uses the `dart:io` WebSocket implementation. ### Constructor - **IOWebSocketChannel** - Creates a new IOWebSocketChannel instance. ### Methods - **ready** - Returns a Future that completes when the channel is ready. - **close** - Closes the WebSocket channel. ### Properties - **closeCode** - The close code sent by the server. - **closeReason** - The close reason sent by the server. - **protocol** - The subprotocol selected by the server. - **pingInterval** - The interval between pings. ### Example ```dart import 'package:web_socket_channel/io.dart'; final channel = IOWebSocketChannel.connect('ws://example.com'); channel.stream.listen((message) { print('Received: $message'); }); channel.sink.add('Hello'); ``` ``` -------------------------------- ### bufferedAmount property Source: https://pub.dev/documentation/web_socket_channel/latest/html/HtmlWebSocketChannel/bufferedAmount.html Documentation for the bufferedAmount property of the WebSocketChannel class. ```APIDOC ## bufferedAmount property ### Description The number of bytes of data that have been queued but not yet transmitted to the network. ### Property `int? get bufferedAmount` ### Implementation ```dart int? get bufferedAmount => innerWebSocket.bufferedAmount; ``` ``` -------------------------------- ### Properties Source: https://pub.dev/documentation/web_socket_channel/latest/adapter_web_socket_channel/AdapterWebSocketChannel-class.html Properties of AdapterWebSocketChannel for accessing connection state and streams. ```APIDOC ## Properties ### closeCode - **closeCode** (int?) - The close code set when the WebSocket connection is closed. No setter. ### closeReason - **closeReason** (String?) - The close reason set when the WebSocket connection is closed. No setter. ### protocol - **protocol** (String?) - The subprotocol selected by the server. No setter. ### ready - **ready** (Future) - A future that completes when the WebSocket connection has been established. No setter. ### sink - **sink** (WebSocketSink) - The sink for sending values to the other endpoint. Late final. ### stream - **stream** (Stream) - The single-subscription stream that emits values from the other endpoint. No setter. ``` -------------------------------- ### HtmlWebSocketChannel(Object webSocket) Source: https://pub.dev/documentation/web_socket_channel/latest/html/HtmlWebSocketChannel-class.html Creates a channel wrapping the given dart:html WebSocket object. ```APIDOC ## HtmlWebSocketChannel(Object webSocket) ### Description Creates a channel wrapping the provided `dart:html` `WebSocket`. ### Constructor `HtmlWebSocketChannel(Object webSocket)` ### Parameters - **webSocket** (Object) - Required - The `dart:html` `WebSocket` to wrap. ``` -------------------------------- ### status.missingMandatoryExtension Source: https://pub.dev/documentation/web_socket_channel/latest/status/status-library.html Constant: The client expected mandatory extensions not negotiated by the server. ```APIDOC ## Constant `missingMandatoryExtension` ### Description The client is terminating the connection because it expected the server to negotiate one or more extensions, but the server didn't return them in the response message of the WebSocket handshake. ### Type const int ### Usage ```dart import 'package:web_socket_channel/status.dart' as status; channel.close(status.missingMandatoryExtension); ``` ``` -------------------------------- ### WebSocketSink.add implementation Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketSink/add.html The implementation of the add method, which delegates the data event to the underlying sink. ```dart @override void add(T data) { _sink.add(data); } ``` -------------------------------- ### WebSocketChannel.signKey(String key) → String Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel-class.html Signs a Sec-WebSocket-Key header sent by a WebSocket client as part of the initial handshake. ```APIDOC ## WebSocketChannel.signKey(String key) ### Description Signs a `Sec-WebSocket-Key` header sent by a WebSocket client as part of the initial handshake. ### Parameters - **key** (String) - Required - The `Sec-WebSocket-Key` header value to sign. ### Return - **String** - The signed key string. ``` -------------------------------- ### HtmlWebSocketChannel Source: https://pub.dev/documentation/web_socket_channel/latest/html Documentation for the HtmlWebSocketChannel class, which provides a WebSocketChannel implementation using dart:html WebSocket. ```APIDOC ## HtmlWebSocketChannel ### Description A WebSocketChannel that communicates using a `dart:html` `WebSocket`. ### Constructor - **HtmlWebSocketChannel({BinaryType binaryType = BinaryType.blob})** - Creates a new HtmlWebSocketChannel with the specified binary type. ### Properties - **binaryType** (BinaryType) - The type of binary messages emitted by the channel. ### Methods - **connect(String url)** - Connects to the WebSocket at the given URL. - **ready** (Future) - A future that completes when the channel is ready. - **close({int? code, String? reason})** - Closes the WebSocket connection. ### Usage Example ```dart final channel = HtmlWebSocketChannel(); await channel.connect('wss://example.com/socket'); channel.stream.listen((message) { print('Received: $message'); }); channel.sink.add('Hello'); ``` ``` -------------------------------- ### status.internalServerError Source: https://pub.dev/documentation/web_socket_channel/latest/status The server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request. ```APIDOC ## status.internalServerError ### Description The server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request. ### Type const int ``` -------------------------------- ### changeSink method Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel/changeSink.html Returns a copy of this StreamChannel with the sink replaced by the return value of the provided change function. ```APIDOC ## changeSink method ### Description Returns a copy of this StreamChannel with the sink replaced by the return value of the provided change function. ### Method changeSink ### Signature dart StreamChannel changeSink(StreamSink Function(StreamSink) change) ### Parameters - **change** (StreamSink Function(StreamSink)) - Required - A function that takes the current sink and returns a new sink. ### Return Value - **StreamChannel** - A new StreamChannel with the sink replaced by the return value of `change`. ### Implementation dart @override StreamChannel changeSink(StreamSink Function(StreamSink) change) => StreamChannel.withCloseGuarantee(stream, change(sink)); ``` -------------------------------- ### status.abnormalClosure Source: https://pub.dev/documentation/web_socket_channel/latest/status The connection was closed abnormally. ```APIDOC ## status.abnormalClosure ### Description The connection was closed abnormally. ### Type const int ``` -------------------------------- ### add(dynamic data) → void Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketSink-class.html Adds a data event to the sink. Inherited from StreamSink. ```APIDOC ## add(dynamic data) → void ### Description Adds a data `event` to the sink. This method is inherited from StreamSink. ### Method add ### Parameters - **data** (dynamic) - Required - The data event to add. ### Returns - **void** ``` -------------------------------- ### Properties Source: https://pub.dev/documentation/web_socket_channel/latest/html/HtmlWebSocketChannel-class.html Properties of HtmlWebSocketChannel that provide information about the WebSocket connection. ```APIDOC ## Properties ### bufferedAmount - **Type**: int? - **Description**: The number of bytes of data that have been queued but not yet transmitted to the network. - **Getter**: yes ### closeCode - **Type**: int? - **Description**: The close code set when the WebSocket connection is closed. - **Getter**: yes ### closeReason - **Type**: String? - **Description**: The close reason set when the WebSocket connection is closed. - **Getter**: yes ### innerWebSocket - **Type**: WebSocket - **Description**: The underlying `dart:html` `WebSocket`. - **Getter**: yes ### protocol - **Type**: String? - **Description**: The subprotocol selected by the server. - **Getter**: yes ### ready - **Type**: Future - **Description**: A future that will complete when the WebSocket connection has been established. - **Getter**: yes ### sink - **Type**: WebSocketSink - **Description**: The sink for sending values to the other endpoint. - **Getter**: yes ### stream - **Type**: Stream - **Description**: The single-subscription stream that emits values from the other endpoint. - **Getter**: yes ``` -------------------------------- ### toString method implementation Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannelException/toString.html Returns a string representation of the exception. If the message is null, it returns 'WebSocketChannelException'; otherwise, it returns 'WebSocketChannelException: $message'. ```dart @override String toString() => message == null ? 'WebSocketChannelException' : 'WebSocketChannelException: $message'; ``` -------------------------------- ### Override ready getter returning _readyCompleter.future Source: https://pub.dev/documentation/web_socket_channel/latest/html/HtmlWebSocketChannel/ready.html Implementation of the `ready` getter that returns the future from `_readyCompleter`. This is the override used in the `WebSocketChannel` class. ```dart @override Future get ready => _readyCompleter.future; ``` -------------------------------- ### close([int? closeCode, String? closeReason]) → Future Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketSink-class.html Closes the web socket connection with an optional close code and reason. ```APIDOC ## close([int? closeCode, String? closeReason]) → Future ### Description Closes the web socket connection. This method is specific to WebSocketSink and allows specifying a close code and reason. ### Method close ### Parameters - **closeCode** (int?) - Optional - The close code to send when closing the connection. - **closeReason** (String?) - Optional - The reason for closing the connection. ### Returns - **Future** - A future that completes when the connection is closed. ``` -------------------------------- ### String toString() Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannelException/toString.html Returns a string representation of the WebSocketChannelException object. The string includes the exception message if it is not null. ```APIDOC ## String toString() ### Description Returns a string representation of this object. For WebSocketChannelException, the string is 'WebSocketChannelException' if the message is null, otherwise 'WebSocketChannelException: $message'. ### Method `toString` ### Return Value - **String** - A string representation of the exception. ### Implementation ```dart @override String toString() => message == null ? 'WebSocketChannelException' : 'WebSocketChannelException: $message'; ``` ``` -------------------------------- ### StreamChannel transform(StreamChannelTransformer transformer) Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel/transform.html Transforms this StreamChannel using the given transformer. This is identical to calling transformer.bind(channel). ```APIDOC ## Dart Method: transform ### Description Transforms the current channel using the given `transformer`. This is equivalent to calling `transformer.bind(channel)`. Returns a new `StreamChannel`. ### Method Signature `StreamChannel transform(StreamChannelTransformer transformer)` ### Parameters - **transformer** (`StreamChannelTransformer`) - Required - The transformer to apply to the channel. ### Return Value - `StreamChannel` - The transformed channel. ### Usage Example ```dart StreamChannel transformed = channel.transform(transformer); ``` ### Implementation ```dart @override StreamChannel transform(StreamChannelTransformer transformer) => transformer.bind(this); ``` ``` -------------------------------- ### status.normalClosure Source: https://pub.dev/documentation/web_socket_channel/latest/status The purpose for which the connection was established has been fulfilled. ```APIDOC ## status.normalClosure ### Description The purpose for which the connection was established has been fulfilled. ### Type const int ``` -------------------------------- ### pipe(StreamChannel other) Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel/pipe.html The pipe method connects this StreamChannel to another, forwarding values in both directions. ```APIDOC ## pipe(StreamChannel other) ### Description Connects this channel to `other`, so that any values emitted by either are sent directly to the other. ### Method pipe ### Signature void pipe(StreamChannel other) ### Parameters - **other** (StreamChannel) - Required - The channel to connect to. ### Implementation ```dart @override void pipe(StreamChannel other) { stream.pipe(other.sink); other.stream.pipe(sink); } ``` ``` -------------------------------- ### status.tlsHandshakeFailed Source: https://pub.dev/documentation/web_socket_channel/latest/status The connection was closed due to a failure to perform a TLS handshake. ```APIDOC ## status.tlsHandshakeFailed ### Description The connection was closed due to a failure to perform a TLS handshake. ### Type const int ``` -------------------------------- ### status.normalClosure Source: https://pub.dev/documentation/web_socket_channel/latest/status/status-library.html Constant: The connection closed normally after fulfilling its purpose. ```APIDOC ## Constant `normalClosure` ### Description The purpose for which the connection was established has been fulfilled. ### Type const int ### Usage ```dart import 'package:web_socket_channel/status.dart' as status; channel.close(status.normalClosure); ``` ``` -------------------------------- ### changeSink implementation Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel/changeSink.html The implementation of changeSink in the StreamChannel class. It uses StreamChannel.withCloseGuarantee to create a new channel with the transformed sink, ensuring proper close behavior. ```dart @override StreamChannel changeSink(StreamSink Function(StreamSink) change) => StreamChannel.withCloseGuarantee(stream, change(sink)); ``` -------------------------------- ### pipe method implementation Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel/pipe.html Overrides the pipe method to connect this channel to another, piping streams in both directions. ```dart @override void pipe(StreamChannel other) { stream.pipe(other.sink); other.stream.pipe(sink); } ``` -------------------------------- ### BinaryType Source: https://pub.dev/documentation/web_socket_channel/latest/html Documentation for the BinaryType enum, which controls the binary message format for HtmlWebSocketChannel. ```APIDOC ## BinaryType ### Description An enum for choosing what type HtmlWebSocketChannel emits for binary messages. ### Values - **blob** - Emits binary messages as Blob objects. - **arrayBuffer** - Emits binary messages as ArrayBuffer objects. ### Usage Example ```dart final channel = HtmlWebSocketChannel(binaryType: BinaryType.arrayBuffer); ``` ``` -------------------------------- ### status.unsupportedData Source: https://pub.dev/documentation/web_socket_channel/latest/status An endpoint is terminating the connection because it has received a type of data it cannot accept. ```APIDOC ## status.unsupportedData ### Description An endpoint is terminating the connection because it has received a type of data it cannot accept. ### Type const int ``` -------------------------------- ### WebSocketChannelException.from(Object? inner) Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannelException/WebSocketChannelException.from.html Creates a WebSocketChannelException from an inner error object, using its string representation as the exception message. ```APIDOC ## WebSocketChannelException.from(Object? inner) ### Description Creates a WebSocketChannelException from an inner error object, using its string representation as the exception message. ### Constructor `WebSocketChannelException.from(Object? inner)` ### Parameters - **inner** (Object?) - Optional - The inner error object whose string representation becomes the exception message. ### Implementation ```dart WebSocketChannelException.from(this.inner) : message = inner.toString(); ``` ``` -------------------------------- ### status.missingMandatoryExtension Source: https://pub.dev/documentation/web_socket_channel/latest/status The client is terminating the connection because it expected the server to negotiate one or more extensions, but the server didn't return them in the response message of the WebSocket handshake. ```APIDOC ## status.missingMandatoryExtension ### Description The client is terminating the connection because it expected the server to negotiate one or more extensions, but the server didn't return them in the response message of the WebSocket handshake. ### Type const int ``` -------------------------------- ### status.protocolError Source: https://pub.dev/documentation/web_socket_channel/latest/status An endpoint is terminating the connection due to a protocol error. ```APIDOC ## status.protocolError ### Description An endpoint is terminating the connection due to a protocol error. ### Type const int ``` -------------------------------- ### WebSocketChannel Properties Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel-class.html Properties of the WebSocketChannel including close code, close reason, subprotocol, readiness, sink, and stream. ```APIDOC ## WebSocketChannel Properties ### closeCode → int? The close code set when the WebSocket connection is closed. Read-only, no setter. ### closeReason → String? The close reason set when the WebSocket connection is closed. Read-only, no setter. ### ready → Future A future that will complete when the WebSocket connection has been established. ### protocol → String? The subprotocol selected by the server. ### sink → WebSocketSink The sink for sending values to the other endpoint. ### stream → Stream The single-subscription stream that emits values from the other endpoint. ``` -------------------------------- ### status.messageTooBig Source: https://pub.dev/documentation/web_socket_channel/latest/status An endpoint is terminating the connection because it has received a message that is too big for it to process. ```APIDOC ## status.messageTooBig ### Description An endpoint is terminating the connection because it has received a message that is too big for it to process. ### Type const int ``` -------------------------------- ### transformSink implementation Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel/transformSink.html The implementation of transformSink, which uses changeSink with the transformer's bind method to transform the sink component. ```dart @override StreamChannel transformSink(StreamSinkTransformer transformer) => changeSink(transformer.bind); ``` -------------------------------- ### StreamChannel.transform implementation Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannel/transform.html Overrides the transform method to delegate to the transformer's bind method, which returns a new StreamChannel. This is the implementation of the transform method for a StreamChannel. ```dart @override StreamChannel transform(StreamChannelTransformer transformer) => transformer.bind(this); ``` -------------------------------- ### WebSocketChannelException.new Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketChannelException/WebSocketChannelException.html Constructs a WebSocketChannelException with an optional message. The exception is used to represent errors that occur during WebSocket channel operations. ```APIDOC ## WebSocketChannelException.new ### Description Creates a new WebSocketChannelException with an optional error message. ### Constructor `WebSocketChannelException([String? message])` ### Parameters - **message** (String?) - Optional - The error message describing the exception. ### Implementation ```dart WebSocketChannelException([this.message]) : inner = null; ``` ``` -------------------------------- ### status.goingAway Source: https://pub.dev/documentation/web_socket_channel/latest/status An endpoint is "going away", such as a server going down or a browser having navigated away from a page. ```APIDOC ## status.goingAway ### Description An endpoint is "going away", such as a server going down or a browser having navigated away from a page. ### Type const int ``` -------------------------------- ### value property Source: https://pub.dev/documentation/web_socket_channel/latest/html/BinaryType/value.html The final String value property that exposes the underlying WebSocket value. ```APIDOC ## value property ### Description The value as understood by the underlying `WebSocket` API. This property is final and of type String. ### Signature ```dart final String value; ``` ### Implementation ```dart content_copy final String value; ``` ``` -------------------------------- ### closeReason getter implementation Source: https://pub.dev/documentation/web_socket_channel/latest/adapter_web_socket_channel/AdapterWebSocketChannel/closeReason.html Overridden getter that returns the close reason. Returns null before the connection is closed. ```dart @override String? get closeReason => _closeReason; ``` -------------------------------- ### BinaryType.list constant implementation Source: https://pub.dev/documentation/web_socket_channel/latest/html/BinaryType/list-constant.html Defines the list constant as a BinaryType instance with the internal name 'list' and the underlying type 'arraybuffer'. Use this constant to configure the channel to emit binary messages as Uint8Lists. ```dart static const list = BinaryType._('list', 'arraybuffer'); ``` -------------------------------- ### addStream method implementation Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketSink/addStream.html Overrides the inherited addStream method to delegate to the underlying sink's addStream. This implementation forwards the stream to the sink and returns the future that completes when the stream is fully added. ```dart content_copy@override Future addStream(Stream stream) => _sink.addStream(stream); ``` -------------------------------- ### done → Future Source: https://pub.dev/documentation/web_socket_channel/latest/web_socket_channel/WebSocketSink-class.html Returns a future that completes when the sink is finished. Inherited from StreamSink. ```APIDOC ## done → Future ### Description Returns a future which is completed when the StreamSink is finished. This property is inherited from StreamSink. ### Returns - **Future** - A future that completes when the sink is done. ``` -------------------------------- ### status.goingAway Source: https://pub.dev/documentation/web_socket_channel/latest/status/status-library.html Constant: The WebSocket close status code indicating an endpoint is 'going away'. ```APIDOC ## Constant `goingAway` ### Description An endpoint is "going away", such as a server going down or a browser having navigated away from a page. ### Type const int ### Usage ```dart import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:web_socket_channel/status.dart' as status; channel.close(status.goingAway); ``` ``` -------------------------------- ### status.policyViolation Source: https://pub.dev/documentation/web_socket_channel/latest/status An endpoint is terminating the connection because it has received a message that violates its policy. ```APIDOC ## status.policyViolation ### Description An endpoint is terminating the connection because it has received a message that violates its policy. ### Type const int ``` -------------------------------- ### policyViolation constant implementation Source: https://pub.dev/documentation/web_socket_channel/latest/status/policyViolation-constant.html Defines the policyViolation constant with the value 1008, representing the WebSocket close code for policy violations. ```dart const policyViolation = 1008; ```