The interface for WebSocket connections.
import 'package:web_socket/src/web_socket.dart';
void main() async {
final socket =
await WebSocket.connect(Uri.parse('wss://ws.postman-echo.com/raw'));
socket.events.listen((e) async {
switch (e) {
case TextDataReceived(text: final text):
print('Received Text: $text');
await socket.close();
case BinaryDataReceived(data: final data):
print('Received Binary: $data');
case CloseReceived(code: final code, reason: final reason):
print('Connection to server closed: $code [$reason]');
}
});
socket.sendText('Hello Dart WebSockets! π');
}
Properties
-
events
β Stream<
WebSocketEvent> -
A Stream of WebSocketEvent received from the peer.
no setter
- hashCode β int
-
The hash code for this object.
no setterinherited
- protocol β String
-
The WebSocket subprotocol negotiated with the peer.
no setter
- runtimeType β Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
close(
[int? code, String? reason]) β Future< void> -
Closes the WebSocket connection and the events
Stream. -
noSuchMethod(
Invocation invocation) β dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
sendBytes(
Uint8List b) β void - Sends binary data to the connected peer.
-
sendText(
String s) β void - Sends text data to the connected peer.
-
toString(
) β String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) β bool -
The equality operator.
inherited