3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
5
import 'dart:async' ;
6
- import 'dart:html ' ;
6
+ import 'dart:js_interop ' ;
7
7
import 'dart:typed_data' ;
8
8
9
9
import 'package:async/async.dart' ;
10
10
import 'package:stream_channel/stream_channel.dart' ;
11
+ import 'package:web/helpers.dart' ;
11
12
12
13
import 'src/channel.dart' ;
13
14
import 'src/exception.dart' ;
15
+ import 'src/web_helpers.dart' ;
14
16
15
17
/// A [WebSocketChannel] that communicates using a `dart:html` [WebSocket] .
16
18
class HtmlWebSocketChannel extends StreamChannelMixin
@@ -73,8 +75,15 @@ class HtmlWebSocketChannel extends StreamChannelMixin
73
75
/// [BinaryType.blob] , they're delivered as [Blob] s instead.
74
76
HtmlWebSocketChannel .connect (Object url,
75
77
{Iterable <String >? protocols, BinaryType ? binaryType})
76
- : this (WebSocket (url.toString (), protocols)
77
- ..binaryType = (binaryType ?? BinaryType .list).value);
78
+ : this (
79
+ WebSocket (
80
+ url.toString (),
81
+ (protocols? .toList () ?? const < String > [])
82
+ .map ((e) => e.toJS)
83
+ .toList ()
84
+ .toJS,
85
+ )..binaryType = (binaryType ?? BinaryType .list).value,
86
+ );
78
87
79
88
/// Creates a channel wrapping [innerWebSocket] .
80
89
HtmlWebSocketChannel (this .innerWebSocket) {
@@ -109,11 +118,7 @@ class HtmlWebSocketChannel extends StreamChannelMixin
109
118
_controller.local.sink.close ();
110
119
});
111
120
112
- innerWebSocket.onMessage.listen ((event) {
113
- var data = event.data;
114
- if (data is ByteBuffer ) data = data.asUint8List ();
115
- _controller.local.sink.add (data);
116
- });
121
+ innerWebSocket.onMessage.listen (_innerListen);
117
122
118
123
// The socket API guarantees that only a single error event will be emitted,
119
124
// and that once it is no other events will be emitted.
@@ -124,16 +129,29 @@ class HtmlWebSocketChannel extends StreamChannelMixin
124
129
});
125
130
}
126
131
132
+ void _innerListen (MessageEvent event) {
133
+ final eventData = event.data;
134
+ Object ? data;
135
+ if (eventData.typeofEquals ('object' ) &&
136
+ (eventData as JSObject ).instanceOfString ('ArrayBuffer' )) {
137
+ data = (eventData as JSArrayBuffer ).toDart.asUint8List ();
138
+ } else {
139
+ data = event.data;
140
+ }
141
+ _controller.local.sink.add (data);
142
+ }
143
+
127
144
/// Pipes user events to [innerWebSocket] .
128
145
void _listen () {
129
- _controller.local.stream.listen (innerWebSocket.send, onDone: () {
146
+ _controller.local.stream.listen ((obj) => innerWebSocket.send (obj! .jsify ()! ),
147
+ onDone: () {
130
148
// On Chrome and possibly other browsers, `null` can't be passed as the
131
149
// default here. The actual arity of the function call must be correct or
132
150
// it will fail.
133
151
if (_localCloseCode != null && _localCloseReason != null ) {
134
- innerWebSocket.close (_localCloseCode, _localCloseReason);
152
+ innerWebSocket.close (_localCloseCode! , _localCloseReason! );
135
153
} else if (_localCloseCode != null ) {
136
- innerWebSocket.close (_localCloseCode);
154
+ innerWebSocket.close (_localCloseCode! );
137
155
} else {
138
156
innerWebSocket.close ();
139
157
}
0 commit comments