FakeWebSocketServer class final

An in-memory WebSocket server for tests. Pass it as WebSocketOptions.transport; every connect creates a FakeWebSocketConnection that the test drives.

Events reach the client asynchronously (in a microtask), like on a real network, so tests can use fake_async or plain await.

final server = FakeWebSocketServer();
final client = WebSocketClient.text(
  options: WebSocketOptions(transport: server),
);
await client.connect('ws://test');
server.last.sendText('hello');
Implemented types

Constructors

FakeWebSocketServer({bool autoAccept = true, String? protocol, bool supportsPing = true})
Creates a server. With autoAccept every connection opens right away; otherwise the test calls FakeWebSocketConnection.accept or FakeWebSocketConnection.reject.

Properties

autoAccept ↔ bool
Whether new connections open immediately.
getter/setter pair
connected → Stream<FakeWebSocketConnection>
New connections, as they are made.
no setter
connections → List<FakeWebSocketConnection>
Every connection made so far, oldest first.
no setter
decodesJson → bool
Whether TransportTextMode.json decodes off the Dart thread.
no setteroverride
hashCode → int
The hash code for this object.
no setterinherited
last → FakeWebSocketConnection
The newest connection.
no setter
name → String
A short name for logs and metrics (dart, web, native, fake).
no setteroverride
protocol ↔ String?
Subprotocol selected when connections are accepted automatically.
getter/setter pair
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
supportsPing → bool
Whether protocol pings are available; false models a browser.
final

Methods

connect(TransportRequest request, TransportListener listener) → TransportConnection
Starts connecting; events arrive at listener.
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