1
- import 'dart:io' ;
2
1
import 'dart:typed_data' ;
3
2
3
+ import 'package:universal_io/io.dart' ;
4
+ import 'package:web_socket_channel/web_socket_channel.dart' ;
5
+
4
6
import 'io/bytes.dart' ;
5
7
import 'rsocket.dart' ;
6
8
@@ -56,14 +58,16 @@ class TcpDuplexConnection extends DuplexConnection {
56
58
}
57
59
58
60
class WebSocketDuplexConnection extends DuplexConnection {
59
- WebSocket webSocket;
60
- bool closed = false ;
61
+ WebSocketChannel webSocket;
62
+ bool closed = true ;
61
63
62
64
WebSocketDuplexConnection (this .webSocket);
63
65
64
66
@override
65
67
void init () {
66
- webSocket.listen ((message) {
68
+
69
+
70
+ webSocket.stream.listen ((message) {
67
71
var data = message as List <int >;
68
72
var frameLenBytes = i24ToBytes (data.length);
69
73
receiveHandler !(Uint8List .fromList (frameLenBytes + data));
@@ -79,15 +83,14 @@ class WebSocketDuplexConnection extends DuplexConnection {
79
83
if (! closed) {
80
84
closed = true ;
81
85
_availability = 0.0 ;
82
- webSocket.close ();
83
86
closeHandler? .call ();
84
87
}
85
88
}
86
89
87
90
@override
88
91
void write (Uint8List chunk) {
89
92
//remove frame length: 3 bytes
90
- webSocket.add (chunk.sublist (3 ));
93
+ webSocket.sink. add (chunk.sublist (3 ));
91
94
}
92
95
}
93
96
@@ -97,10 +100,13 @@ Future<DuplexConnection> connectRSocket(String url, TcpChunkHandler handler) {
97
100
if (scheme == 'tcp' ) {
98
101
var socketFuture = Socket .connect (uri.host, uri.port);
99
102
return socketFuture.then ((socket) => TcpDuplexConnection (socket));
100
- } else if (scheme == 'ws' || scheme == 'wss' ) {
101
- var socketFuture = WebSocket .connect (url);
102
- return socketFuture.then ((socket) => WebSocketDuplexConnection (socket));
103
+ }if (scheme == 'ws' || scheme == 'wss' ) {
104
+ final websocket = WebSocketChannel .connect (
105
+ Uri .parse (url),
106
+ );
107
+ return Future .value (WebSocketDuplexConnection (websocket));
103
108
} else {
104
109
return Future .error ('${scheme } unsupported' );
105
110
}
106
111
}
112
+
0 commit comments