@@ -10,9 +10,10 @@ import 'package:crypto/crypto.dart';
10
10
import 'package:stream_channel/stream_channel.dart' ;
11
11
12
12
import '../web_socket_adapter_web_socket_channel.dart' ;
13
- import 'copy/web_socket_impl.dart' ;
14
13
import 'exception.dart' ;
15
14
15
+ const String _webSocketGUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' ;
16
+
16
17
/// A [StreamChannel] that communicates over a WebSocket.
17
18
///
18
19
/// This is implemented by classes that use `dart:io` and `dart:html` .
@@ -21,33 +22,27 @@ import 'exception.dart';
21
22
///
22
23
/// All implementations emit [WebSocketChannelException] s. These exceptions wrap
23
24
/// the native exception types where possible.
24
- class WebSocketChannel extends StreamChannelMixin {
25
- /// The underlying web socket.
26
- ///
27
- /// This is essentially a copy of `dart:io` 's WebSocket implementation, with
28
- /// the IO-specific pieces factored out.
29
- final WebSocketImpl _webSocket;
30
-
25
+ abstract interface class WebSocketChannel extends StreamChannelMixin {
31
26
/// The subprotocol selected by the server.
32
27
///
33
28
/// For a client socket, this is initially `null` . After the WebSocket
34
29
/// connection is established the value is set to the subprotocol selected by
35
30
/// the server. If no subprotocol is negotiated the value will remain `null` .
36
- String ? get protocol => _webSocket.protocol ;
31
+ String ? get protocol;
37
32
38
33
/// The [close code][] set when the WebSocket connection is closed.
39
34
///
40
35
/// [close code] : https://tools.ietf.org/html/rfc6455#section-7.1.5
41
36
///
42
37
/// Before the connection has been closed, this will be `null` .
43
- int ? get closeCode => _webSocket.closeCode ;
38
+ int ? get closeCode;
44
39
45
40
/// The [close reason][] set when the WebSocket connection is closed.
46
41
///
47
42
/// [close reason] : https://tools.ietf.org/html/rfc6455#section-7.1.6
48
43
///
49
44
/// Before the connection has been closed, this will be `null` .
50
- String ? get closeReason => _webSocket.closeReason ;
45
+ String ? get closeReason;
51
46
52
47
/// A future that will complete when the WebSocket connection has been
53
48
/// established.
@@ -74,17 +69,14 @@ class WebSocketChannel extends StreamChannelMixin {
74
69
/// // send data.
75
70
/// channel.sink.add('Hello World');
76
71
/// ```
77
- final Future <void > ready = Future .value ();
78
-
79
- @override
80
- Stream get stream => StreamView (_webSocket);
72
+ Future <void > get ready;
81
73
82
74
/// The sink for sending values to the other endpoint.
83
75
///
84
76
/// This supports additional arguments to [WebSocketSink.close] that provide
85
77
/// the remote endpoint reasons for closing the connection.
86
78
@override
87
- WebSocketSink get sink => WebSocketSink ._(_webSocket) ;
79
+ WebSocketSink get sink;
88
80
89
81
/// Signs a `Sec-WebSocket-Key` header sent by a WebSocket client as part of
90
82
/// the [initial handshake][] .
@@ -98,33 +90,7 @@ class WebSocketChannel extends StreamChannelMixin {
98
90
// [key] is expected to be base64 encoded, and so will be pure ASCII.
99
91
=>
100
92
convert.base64
101
- .encode (sha1.convert ((key + webSocketGUID).codeUnits).bytes);
102
-
103
- /// Creates a new WebSocket handling messaging across an existing [channel] .
104
- ///
105
- /// This is a cross-platform constructor; it doesn't use either `dart:io` or
106
- /// `dart:html` . It's also HTTP-API-agnostic, which means that the initial
107
- /// [WebSocket handshake][] must have already been completed on the socket
108
- /// before this is called.
109
- ///
110
- /// [protocol] should be the protocol negotiated by this handshake, if any.
111
- ///
112
- /// [pingInterval] controls the interval for sending ping signals. If a ping
113
- /// message is not answered by a pong message from the peer, the WebSocket is
114
- /// assumed disconnected and the connection is closed with a `goingAway` close
115
- /// code. When a ping signal is sent, the pong message must be received within
116
- /// [pingInterval] . It defaults to `null` , indicating that ping messages are
117
- /// disabled.
118
- ///
119
- /// If this is a WebSocket server, [serverSide] should be `true` (the
120
- /// default); if it's a client, [serverSide] should be `false` .
121
- ///
122
- /// [WebSocket handshake] : https://tools.ietf.org/html/rfc6455#section-4
123
- WebSocketChannel (StreamChannel <List <int >> channel,
124
- {String ? protocol, Duration ? pingInterval, bool serverSide = true })
125
- : _webSocket = WebSocketImpl .fromSocket (
126
- channel.stream, channel.sink, protocol, serverSide)
127
- ..pingInterval = pingInterval;
93
+ .encode (sha1.convert ((key + _webSocketGUID).codeUnits).bytes);
128
94
129
95
/// Creates a new WebSocket connection.
130
96
///
@@ -138,19 +104,15 @@ class WebSocketChannel extends StreamChannelMixin {
138
104
/// The [ready] future will complete after the channel is connected.
139
105
/// If there are errors creating the connection the [ready] future will
140
106
/// complete with an error.
141
- factory WebSocketChannel . connect (Uri uri, {Iterable <String >? protocols}) =>
107
+ static WebSocketChannel connect (Uri uri, {Iterable <String >? protocols}) =>
142
108
WebSocketAdapterWebSocketChannel .connect (uri, protocols: protocols);
143
109
}
144
110
145
111
/// The sink exposed by a [WebSocketChannel] .
146
112
///
147
113
/// This is like a normal [StreamSink] , except that it supports extra arguments
148
114
/// to [close] .
149
- class WebSocketSink extends DelegatingStreamSink {
150
- final WebSocketImpl _webSocket;
151
-
152
- WebSocketSink ._(WebSocketImpl super .webSocket) : _webSocket = webSocket;
153
-
115
+ abstract interface class WebSocketSink implements DelegatingStreamSink {
154
116
/// Closes the web socket connection.
155
117
///
156
118
/// [closeCode] and [closeReason] are the [close code][] and [reason][] sent
@@ -160,6 +122,5 @@ class WebSocketSink extends DelegatingStreamSink {
160
122
/// [close code] : https://tools.ietf.org/html/rfc6455#section-7.1.5
161
123
/// [reason] : https://tools.ietf.org/html/rfc6455#section-7.1.6
162
124
@override
163
- Future close ([int ? closeCode, String ? closeReason]) =>
164
- _webSocket.close (closeCode, closeReason);
125
+ Future close ([int ? closeCode, String ? closeReason]);
165
126
}
0 commit comments