HtmlWebSocketChannel constructor
HtmlWebSocketChannel(
- WebSocket _webSocket
Creates a channel wrapping _webSocket
.
Implementation
HtmlWebSocketChannel(this._webSocket) {
if (_webSocket.readyState == WebSocket.OPEN) {
_listen();
} else {
// The socket API guarantees that only a single open event will be
// emitted.
_webSocket.onOpen.first.then((_) {
_listen();
});
}
// The socket API guarantees that only a single error event will be emitted,
// and that once it is no open or message events will be emitted.
_webSocket.onError.first.then((_) {
_controller.local.sink
.addError(WebSocketChannelException('WebSocket connection failed.'));
_controller.local.sink.close();
});
_webSocket.onMessage.listen((event) {
var data = event.data;
if (data is ByteBuffer) data = data.asUint8List();
_controller.local.sink.add(data);
});
// The socket API guarantees that only a single error event will be emitted,
// and that once it is no other events will be emitted.
_webSocket.onClose.first.then((event) {
_closeCode = event.code;
_closeReason = event.reason;
_controller.local.sink.close();
});
}