WebSocket class
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 thesend 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 aMessageEvent objectas its sole argument.You can also define open, close, and error handlers,as specified byEvents.
For more information, see theWebSocketssection of the library tour andIntroducing WebSockets,an HTML5Rocks.com tutorial.
- Inheritance
- Object
- EventTarget
- WebSocket
- Annotations
- @SupportedBrowser.new(SupportedBrowser.CHROME)
- @SupportedBrowser.new(SupportedBrowser.FIREFOX)
- @SupportedBrowser.new(SupportedBrowser.IE, '10')
- @SupportedBrowser.new(SupportedBrowser.SAFARI)
- @Unstable.new()
- @Native.new("WebSocket")
Constructors
Properties
- binaryType↔String?
- getter/setter pair
- bufferedAmount→int?
- no setter
- extensions→String?
- no setter
- hashCode→int
- The hash code for this object.no setterinherited
- on→Events
- This is an ease-of-use accessor for event streams which should only beused when an explicit accessor is not available.no setterinherited
- onClose→Stream<
CloseEvent> - Stream of
closeevents handled by thisWebSocket.no setter - onError→Stream<
Event> - Stream of
errorevents handled by thisWebSocket.no setter - onMessage→Stream<
MessageEvent> - Stream of
messageevents handled by thisWebSocket.no setter - onOpen→Stream<
Event> - Stream of
openevents handled by thisWebSocket.no setter - protocol→String?
- no setter
- readyState→int
- no setter
- runtimeType→Type
- A representation of the runtime type of the object.no setterinherited
- url→String?
- no setter
Methods
- addEventListener(
Stringtype,EventListener?listener, [bool?useCapture])→ void - inherited
- close(
[int?code,String?reason])→ void - dispatchEvent(
Eventevent)→bool - inherited
- noSuchMethod(
Invocationinvocation)→ dynamic - Invoked when a nonexistent method or property is accessed.inherited
- removeEventListener(
Stringtype,EventListener?listener, [bool?useCapture])→ void - inherited
- send(
dynamicdata)→ void - Transmit data to the server over this connection.
- sendBlob(
Blobdata)→ void - Transmit data to the server over this connection.
- sendByteBuffer(
ByteBufferdata)→ void - Transmit data to the server over this connection.
- sendString(
Stringdata)→ void - Transmit data to the server over this connection.
- sendTypedData(
TypedDatadata)→ void - Transmit data to the server over this connection.
- toString(
)→String - A string representation of this object.inherited
Operators
- operator ==(
Objectother)→bool - The equality operator.inherited
Static Properties
Constants
- CLOSED→ constint
- closeEvent→ constEventStreamProvider<
CloseEvent> - Static factory designed to expose
closeevents to eventhandlers that are not necessarily instances ofWebSocket. - CLOSING→ constint
- CONNECTING→ constint
- errorEvent→ constEventStreamProvider<
Event> - Static factory designed to expose
errorevents to eventhandlers that are not necessarily instances ofWebSocket. - messageEvent→ constEventStreamProvider<
MessageEvent> - Static factory designed to expose
messageevents to eventhandlers that are not necessarily instances ofWebSocket. - OPEN→ constint
- openEvent→ constEventStreamProvider<
Event> - Static factory designed to expose
openevents to eventhandlers that are not necessarily instances ofWebSocket.