W3cubDocs

/Dart 2

WebSocket class

Use the WebSocket interface to connect to a WebSocket, and to send and receive data on that WebSocket.

To use a WebSocket in your web app, first create a WebSocket object, passing the WebSocket URL as an argument to the constructor.

var webSocket = new WebSocket('ws://127.0.0.1:1337/ws');

To send data on the WebSocket, use the send method.

if (webSocket != null && webSocket.readyState == WebSocket.OPEN) {
  webSocket.send(data);
} else {
  print('WebSocket not connected, message $data not sent');
}

To receive data on the WebSocket, register a listener for message events.

webSocket.onMessage.listen((MessageEvent e) {
  receivedData(e.data);
});

The message event handler receives a MessageEvent object as its sole argument. You can also define open, close, and error handlers, as specified by WebSocketEvents.

For more information, see the WebSockets section of the library tour and Introducing WebSockets, an HTML5Rocks.com tutorial.

Inheritance
Annotations
  • @SupportedBrowser(SupportedBrowser.CHROME)
  • @SupportedBrowser(SupportedBrowser.FIREFOX)
  • @SupportedBrowser(SupportedBrowser.IE, '10')
  • @SupportedBrowser(SupportedBrowser.SAFARI)
  • @Unstable()
  • @Native("WebSocket")

Constructors

WebSocket(String url, [ Object protocols ])
factory

Properties

binaryTypeString
read / write
bufferedAmountint
final
extensionsString
final
onCloseStream<CloseEvent>
read-only
Stream of close events handled by this WebSocket.
onErrorStream<Event>
read-only
Stream of error events handled by this WebSocket.
onMessageStream<MessageEvent>
read-only
Stream of message events handled by this WebSocket.
onOpenStream<Event>
read-only
Stream of open events handled by this WebSocket.
protocolString
final
readyStateint
final
urlString
final
hashCodeint
read-only, inherited
The hash code for this object. [...]
onEvents
read-only, inherited
This is an ease-of-use accessor for event streams which should only be used when an explicit accessor is not available.
runtimeTypeType
read-only, inherited
A representation of the runtime type of the object.

Methods

close([int code, String reason ]) → void
send(dynamic data) → void
Transmit data to the server over this connection. [...]
sendBlob(Blob data) → void
@JSName('send')
Transmit data to the server over this connection. [...]
sendByteBuffer(ByteBuffer data) → void
@JSName('send')
Transmit data to the server over this connection. [...]
sendString(String data) → void
@JSName('send')
Transmit data to the server over this connection. [...]
sendTypedData(TypedData data) → void
@JSName('send')
Transmit data to the server over this connection. [...]
addEventListener(String type, EventListener listener, [ bool useCapture ]) → void
inherited
dispatchEvent(Event event) → bool
inherited
noSuchMethod(Invocation invocation) → dynamic
inherited
Invoked when a non-existent method or property is accessed. [...]
removeEventListener(String type, EventListener listener, [ bool useCapture ]) → void
inherited
toString() → String
inherited
Returns a string representation of this object.

Operators

operator ==(dynamic other) → bool
inherited
The equality operator. [...]

Static Properties

supportedbool
read-only
Checks if this type is supported on the current platform.

Constants

CLOSED → const int
3
closeEvent → const EventStreamProvider<CloseEvent>
Static factory designed to expose close events to event handlers that are not necessarily instances of WebSocket. [...]
const EventStreamProvider<CloseEvent>('close')
CLOSING → const int
2
CONNECTING → const int
0
errorEvent → const EventStreamProvider<Event>
Static factory designed to expose error events to event handlers that are not necessarily instances of WebSocket. [...]
const EventStreamProvider<Event>('error')
messageEvent → const EventStreamProvider<MessageEvent>
Static factory designed to expose message events to event handlers that are not necessarily instances of WebSocket. [...]
const EventStreamProvider<MessageEvent>('message')
OPEN → const int
1
openEvent → const EventStreamProvider<Event>
Static factory designed to expose open events to event handlers that are not necessarily instances of WebSocket. [...]
const EventStreamProvider<Event>('open')

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.5.0/dart-html/WebSocket-class.html