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.
- Annotations
- DocsEditable()
- DomName('WebSocket')
- SupportedBrowser(SupportedBrowser.CHROME)
- SupportedBrowser(SupportedBrowser.FIREFOX)
- SupportedBrowser(SupportedBrowser.IE, '10')
- SupportedBrowser(SupportedBrowser.SAFARI)
- Unstable()
- Extends
- Object
- EventTarget
- WebSocket
Constants
- int CLOSED = 3
-
const
-
EventStreamProvider<CloseEvent>
closeEvent
=
const EventStreamProvider<CloseEvent>
('close') -
constStatic factory designed to expose
close
events to event handlers that are not necessarily instances of WebSocket. - int CLOSING = 2
-
const
- int CONNECTING = 0
-
const
-
EventStreamProvider<Event>
errorEvent
=
const EventStreamProvider<Event>
('error') -
constStatic factory designed to expose
error
events to event handlers that are not necessarily instances of WebSocket. -
EventStreamProvider<MessageEvent>
messageEvent
=
const EventStreamProvider<MessageEvent>
('message') -
constStatic factory designed to expose
message
events to event handlers that are not necessarily instances of WebSocket. - int OPEN = 1
-
const
-
EventStreamProvider<Event>
openEvent
=
const EventStreamProvider<Event>
('open') -
constStatic factory designed to expose
open
events to event handlers that are not necessarily instances of WebSocket.
Static Properties
- bool supported
-
read-onlyChecks if this type is supported on the current platform.
Properties
- String binaryType
-
read / write
- int bufferedAmount
-
read-only
- String extensions
-
read-only
- Events on
-
read-only, inheritedThis is an ease-of-use accessor for event streams which should only be used when an explicit accessor is not available.
- Stream<CloseEvent> onClose
-
read-onlyStream of
close
events handled by thisWebSocket
. - Stream<Event> onError
-
read-onlyStream of
error
events handled by thisWebSocket
. - Stream<MessageEvent> onMessage
-
read-onlyStream of
message
events handled by thisWebSocket
. - Stream<Event> onOpen
-
read-onlyStream of
open
events handled by thisWebSocket
. - String protocol
-
read-only
- int readyState
-
read-only
- String url
-
read-only
Constructors
- WebSocket(String url, [protocol_OR_protocols])
Methods
-
addEventListener(
String type, dynamic listener(Event event), [bool useCapture]) → void -
inherited
-
close(
[int code, String reason]) → void -
dispatchEvent(
Event event) → bool -
inherited
-
removeEventListener(
String type, dynamic 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