BrowserWebSocketTransport class

The WebSocketTransport for code running in a browser (Flutter web, Dart compiled to JavaScript or WebAssembly), built on the browser's WebSocket through dart:js_interop.

platformWebSocketTransport() returns one in the browser, so applications rarely create it. The browser's WebSocket cannot send custom headers with the handshake, so connect with a non-empty headers fails at once with a WebSocketException: put a token in the URL or in a protocol instead. Binary messages arrive as Uint8List. The browser reports no detail when a connection fails, so the exception says only cannot connect, with the close code when there is one.

import 'package:spring_web_dart/browser.dart';

// The token travels in the URL, since a browser cannot send headers.
final transport = BrowserWebSocketTransport();
final session = await transport.connect(
  Uri.parse('wss://chat.neon.books/ws?token=$token'),
);

Spring (Java): no browser in Java; on the browser side Spring applications use JavaScript (WebSocket, SockJS, STOMP.js).

Implemented types

Constructors

BrowserWebSocketTransport()
A transport on WebSocket. It has no settings: the browser decides about proxies and certificates.

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

connect(Uri url, {HttpHeaders? headers, List<String> protocols = const [], Duration? timeout}) → Future<WebSocketSession>
Opens a connection to url (ws: or wss:) and completes once the handshake has succeeded.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited