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
- DartHtmlDomObject
- EventTarget
- WebSocket
- Annotations
- DocsEditable()
- DomName('WebSocket')
- SupportedBrowser(SupportedBrowser.CHROME)
- SupportedBrowser(SupportedBrowser.FIREFOX)
- SupportedBrowser(SupportedBrowser.IE, '10')
- SupportedBrowser(SupportedBrowser.SAFARI)
- Unstable()
Constants
- CLOSED → int
-
const
3 - closeEvent → EventStreamProvider<CloseEvent>
-
Static factory designed to expose
closeevents to event handlers that are not necessarily instances of WebSocket.…constconst EventStreamProvider<CloseEvent>('close') - CLOSING → int
-
const
2 - CONNECTING → int
-
const
0 - errorEvent → EventStreamProvider<Event>
-
Static factory designed to expose
errorevents to event handlers that are not necessarily instances of WebSocket.…constconst EventStreamProvider<Event>('error') - messageEvent → EventStreamProvider<MessageEvent>
-
Static factory designed to expose
messageevents to event handlers that are not necessarily instances of WebSocket.…constconst EventStreamProvider<MessageEvent>('message') - OPEN → int
-
const
1 - openEvent → EventStreamProvider<Event>
-
Static factory designed to expose
openevents to event handlers that are not necessarily instances of WebSocket.…constconst EventStreamProvider<Event>('open')
Static Properties
Static Methods
Constructors
- WebSocket(String url, [protocol_OR_protocols])
-
factory
- WebSocket.internal_()
Properties
- binaryType → String
-
read / write
- blink_jsObject → JsObject
-
The underlying JS DOM object.
read / write, inherited - bufferedAmount → int
-
read-only
- extensions → String
-
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 - onClose → Stream<CloseEvent>
-
Stream of
closeevents handled by thisWebSocket.read-only - onError → Stream<Event>
-
Stream of
errorevents handled by thisWebSocket.read-only - onMessage → Stream<MessageEvent>
-
Stream of
messageevents handled by thisWebSocket.read-only - onOpen → Stream<Event>
-
Stream of
openevents handled by thisWebSocket.read-only - protocol → String
-
read-only
- readyState → int
-
read-only
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited - url → String
-
read-only
Operators
-
operator ==(
other) → bool -
The equality operator.…
inherited
Methods
-
addEventListener(
String type, EventListener listener(Event event), [bool useCapture]) → void -
inherited
-
close(
[int code, String reason]) → void -
dispatchEvent(
Event event) → bool -
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed.…
inherited -
removeEventListener(
String type, EventListener listener(Event event), [bool useCapture]) → void -
inherited
-
send(
data) → void -
sendBlob(
Blob data) → void -
sendByteBuffer(
ByteBuffer data) → void -
sendString(
String data) → void -
sendTypedData(
TypedData data) → void -
toString(
) → String -
Returns a string representation of this object.
inherited