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
- Object
- JSObject
- DartHtmlDomObject
- EventTarget
- WebSocket
- Annotations
- @DocsEditable()
- @DomName('WebSocket')
- @SupportedBrowser(SupportedBrowser.CHROME)
- @SupportedBrowser(SupportedBrowser.FIREFOX)
- @SupportedBrowser(SupportedBrowser.IE, '10')
- @SupportedBrowser(SupportedBrowser.SAFARI)
- @Unstable()
Constants
- CLOSED → int
-
@DocsEditable(), @DomName('WebSocket.CLOSED')
3 - closeEvent → EventStreamProvider<CloseEvent>
-
Static factory designed to expose
closeevents to event handlers that are not necessarily instances of WebSocket.@DocsEditable(), @DomName('WebSocket.closeEvent')const EventStreamProvider<CloseEvent>('close') - CLOSING → int
-
@DocsEditable(), @DomName('WebSocket.CLOSING')
2 - CONNECTING → int
-
@DocsEditable(), @DomName('WebSocket.CONNECTING')
0 - errorEvent → EventStreamProvider<Event>
-
Static factory designed to expose
errorevents to event handlers that are not necessarily instances of WebSocket.@DocsEditable(), @DomName('WebSocket.errorEvent')const EventStreamProvider<Event>('error') - messageEvent → EventStreamProvider<MessageEvent>
-
Static factory designed to expose
messageevents to event handlers that are not necessarily instances of WebSocket.@DocsEditable(), @DomName('WebSocket.messageEvent')const EventStreamProvider<MessageEvent>('message') - OPEN → int
-
@DocsEditable(), @DomName('WebSocket.OPEN')
1 - openEvent → EventStreamProvider<Event>
-
Static factory designed to expose
openevents to event handlers that are not necessarily instances of WebSocket.@DocsEditable(), @DomName('WebSocket.openEvent')const EventStreamProvider<Event>('open')
Static Properties
- instanceRuntimeType → Type
-
@Deprecated("Internal Use Only"), read-only
- supported → bool
-
Checks if this type is supported on the current platform.
read-only
Constructors
- WebSocket(String url, [ Object protocols ])
-
factory
- WebSocket.internal_()
Properties
- binaryType → String
-
@DocsEditable(), @DomName('WebSocket.binaryType'), read / write
- bufferedAmount → int
-
@DocsEditable(), @DomName('WebSocket.bufferedAmount'), read-only
- extensions → String
-
@DocsEditable(), @DomName('WebSocket.extensions'), read-only
- onClose → Stream<CloseEvent>
-
Stream of
closeevents handled by thisWebSocket.@DocsEditable(), @DomName('WebSocket.onclose'), read-only - onError → Stream<Event>
-
Stream of
errorevents handled by thisWebSocket.@DocsEditable(), @DomName('WebSocket.onerror'), read-only - onMessage → Stream<MessageEvent>
-
Stream of
messageevents handled by thisWebSocket.@DocsEditable(), @DomName('WebSocket.onmessage'), read-only - onOpen → Stream<Event>
-
Stream of
openevents handled by thisWebSocket.@DocsEditable(), @DomName('WebSocket.onopen'), read-only - protocol → String
-
@DocsEditable(), @DomName('WebSocket.protocol'), read-only
- readyState → int
-
@DocsEditable(), @DomName('WebSocket.readyState'), read-only
- url → String
-
@DocsEditable(), @DomName('WebSocket.url'), read-only
- hashCode → int
-
read-only, inherited
- on → Events
-
This is an ease-of-use accessor for event streams which should only be used when an explicit accessor is not available.
read-only, inherited - runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
Operators
-
operator ==(
other) → bool -
The equality operator.
inherited
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 -
Invoked when a non-existent method or property is accessed.
inherited -
removeEventListener(
String type, EventListener listener, [ bool useCapture ]) → void -
inherited
-
toString(
) → String -
Returns the result of the JavaScript objects
toStringmethod.inherited