WebSocket class - dart:html library (original) (raw)
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 Events.
For more information, see theWebSocketssection of the library tour andIntroducing WebSockets, an HTML5Rocks.com tutorial.
Inheritance
- Object
- EventTarget
- WebSocket
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
binaryType ↔ String?
getter/setter pair
no setter
extensions → String?
no setter
The hash code for this object.
no setterinherited
This is an ease-of-use accessor for event streams which should only be used when an explicit accessor is not available.
no setterinherited
Stream of close
events handled by this WebSocket.
no setter
Stream of error
events handled by this WebSocket.
no setter
onMessage → Stream<MessageEvent>
Stream of message
events handled by this WebSocket.
no setter
Stream of open
events handled by this WebSocket.
no setter
no setter
no setter
A representation of the runtime type of the object.
no setterinherited
no setter
Methods
addEventListener(String type, EventListener? listener, [bool? useCapture])→ void
inherited
close([int? code, String? reason])→ void
dispatchEvent(Event event)→ bool
inherited
noSuchMethod(Invocation invocation)→ dynamic
Invoked when a nonexistent method or property is accessed.
inherited
removeEventListener(String type, EventListener? listener, [bool? useCapture])→ void
inherited
send(dynamic data)→ void
Transmit data to the server over this connection.
Transmit data to the server over this connection.
sendByteBuffer(ByteBuffer data)→ void
Transmit data to the server over this connection.
sendString(String data)→ void
Transmit data to the server over this connection.
sendTypedData(TypedData data)→ void
Transmit data to the server over this connection.
A string representation of this object.
inherited
Operators
operator ==(Object other)→ bool
The equality operator.
inherited
Static Properties
Checks if this type is supported on the current platform.
no setter
Constants
closeEvent → const EventStreamProvider<CloseEvent>
Static factory designed to expose close
events to event handlers that are not necessarily instances of WebSocket.
CONNECTING → const int
errorEvent → const EventStreamProvider<Event>
Static factory designed to expose error
events to event handlers that are not necessarily instances of WebSocket.
messageEvent → const EventStreamProvider<MessageEvent>
Static factory designed to expose message
events to event handlers that are not necessarily instances of WebSocket.
openEvent → const EventStreamProvider<Event>
Static factory designed to expose open
events to event handlers that are not necessarily instances of WebSocket.