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
  • @DocsEditable()
  • @DomName('WebSocket')
  • @SupportedBrowser(SupportedBrowser.CHROME)
  • @SupportedBrowser(SupportedBrowser.FIREFOX)
  • @SupportedBrowser(SupportedBrowser.IE, '10')
  • @SupportedBrowser(SupportedBrowser.SAFARI)
  • @Unstable()

Constants

CLOSEDint
@DocsEditable(), @DomName('WebSocket.CLOSED')
3
closeEventEventStreamProvider<CloseEvent>
@DocsEditable(), @DomName('WebSocket.closeEvent')

Static factory designed to expose close events to event handlers that are not necessarily instances of WebSocket.

CLOSINGint
@DocsEditable(), @DomName('WebSocket.CLOSING')
2
CONNECTINGint
@DocsEditable(), @DomName('WebSocket.CONNECTING')
0
errorEventEventStreamProvider<Event>
@DocsEditable(), @DomName('WebSocket.errorEvent')

Static factory designed to expose error events to event handlers that are not necessarily instances of WebSocket.

const EventStreamProvider<Event>('error')
messageEventEventStreamProvider<MessageEvent>
@DocsEditable(), @DomName('WebSocket.messageEvent')

Static factory designed to expose message events to event handlers that are not necessarily instances of WebSocket.

const EventStreamProvider<MessageEvent>('message')
OPENint
@DocsEditable(), @DomName('WebSocket.OPEN')
1
openEventEventStreamProvider<Event>
@DocsEditable(), @DomName('WebSocket.openEvent')

Static factory designed to expose open events to event handlers that are not necessarily instances of WebSocket.

const EventStreamProvider<Event>('open')

Static Properties

instanceRuntimeTypeType
@Deprecated("Internal Use Only"), read-only
supportedbool
read-only

Checks if this type is supported on the current platform.

Constructors

WebSocket(String url, [ Object protocols ])
factory
WebSocket.internal_()

Properties

binaryTypeString
@DocsEditable(), @DomName('WebSocket.binaryType'), read / write
bufferedAmountint
@DocsEditable(), @DomName('WebSocket.bufferedAmount'), read-only
extensionsString
@DocsEditable(), @DomName('WebSocket.extensions'), read-only
onCloseStream<CloseEvent>
@DocsEditable(), @DomName('WebSocket.onclose'), read-only

Stream of close events handled by this WebSocket.

onErrorStream<Event>
@DocsEditable(), @DomName('WebSocket.onerror'), read-only

Stream of error events handled by this WebSocket.

onMessageStream<MessageEvent>
@DocsEditable(), @DomName('WebSocket.onmessage'), read-only

Stream of message events handled by this WebSocket.

onOpenStream<Event>
@DocsEditable(), @DomName('WebSocket.onopen'), read-only

Stream of open events handled by this WebSocket.

protocolString
@DocsEditable(), @DomName('WebSocket.protocol'), read-only
readyStateint
@DocsEditable(), @DomName('WebSocket.readyState'), read-only
urlString
@DocsEditable(), @DomName('WebSocket.url'), read-only
hashCodeint
read-only, inherited
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.

Operators

operator ==(other) → bool
inherited

The equality operator.

Methods

close([int code, String reason ]) → void
send(data) → void
sendBlob(Blob data) → void
@DocsEditable(), @DomName('WebSocket.sendBlob')
sendByteBuffer(ByteBuffer data) → void
@DocsEditable(), @DomName('WebSocket.sendByteBuffer')
sendString(String data) → void
@DocsEditable(), @DomName('WebSocket.sendString')
sendTypedData(TypedData data) → void
@DocsEditable(), @DomName('WebSocket.sendTypedData')
addEventListener(String type, EventListener listener, [ bool useCapture ]) → void
inherited
dispatchEvent(Event event) → bool
@DocsEditable(), @DomName('EventTarget.dispatchEvent'), 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 the result of the JavaScript objects toString method.

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