WebSocketSession class abstract interface

One open WebSocket connection: it receives messages as a stream, sends messages, and closes.

A client gets it from WebSocketTransport.connect. A server gets it when it accepts a connection (ServerHttpRequest.upgradeToWebSocket), so the same type serves both sides. With the WebSocket starter you rarely hold one: @WebSocketClient interfaces and @WebSocketEndpoint classes hide it behind typed methods. You use it directly with a bare transport, or in a server handler that needs the connection itself.

session.messages.listen((message) {
  if (message is String) { ... } // A text frame.
  if (message is Uint8List) { ... } // A binary frame.
});
session.sendMessage('{"text":"hi"}');
// Tell the server the client is leaving, not failing.
await session.close(CloseStatus.goingAway);

Spring (Java): WebSocketSession; the incoming messages go to a WebSocketHandler instead of a stream:

session.sendMessage(new TextMessage("{\"text\":\"hi\"}"));
session.close(CloseStatus.GOING_AWAY);
Implementers

Properties

acceptedProtocol → String?
The subprotocol the two sides agreed on in the handshake, or null when none was asked for or accepted.
no setter
closeStatus → CloseStatus?
How the connection was closed, by either side: the code and reason of the close frame. null while it is open.
no setter
hashCode → int
The hash code for this object.
no setterinherited
isOpen → bool
Whether messages can still be sent: false once either side has started to close the connection.
no setter
messages → Stream<Object>
The incoming messages: a String for each text frame, a Uint8List for each binary one. It can be listened to once, and it is done when the connection closes, whichever side closed it.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
uri → Uri
The URL of the connection: the one a client connected to, or the ws: form of the request URL on a server.
no setter

Methods

close([CloseStatus status = CloseStatus.normal]) → Future<void>
Closes the connection, sending status (CloseStatus.normal by default) to the other side. The future completes when the connection is closed.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
sendMessage(Object message) → void
Sends message as a text frame when it is a String, or as a binary frame when it is a List<int>. Throws a WebSocketException when the session is no longer open, and an ArgumentError for any other type. It does not wait for the frame to leave: the message waits in memory until the network takes it. On dart:io a session whose other side takes nothing gives up past its limits (512 KB waiting, or 10 seconds) and drops the connection, and IoWebSocketSession.flush waits until what was sent has left; see IoWebSocketSession.
toString() → String
A string representation of this object.
inherited

Operators

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