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.
close
events handled by this WebSocket.
error
events handled by this WebSocket.
message
events handled by this WebSocket.
open
events handled by this WebSocket.
3
close
events to event
handlers that are not necessarily instances of WebSocket. [...]
const EventStreamProvider<CloseEvent>('close')
2
0
error
events to event
handlers that are not necessarily instances of WebSocket. [...]
const EventStreamProvider<Event>('error')
message
events to event
handlers that are not necessarily instances of WebSocket. [...]
const EventStreamProvider<MessageEvent>('message')
1
open
events to event
handlers that are not necessarily instances of WebSocket. [...]
const EventStreamProvider<Event>('open')