Skip to content

Commit 510f396

Browse files
authored
Remove WebSocketChannel constructor (dart-archive/web_socket_channel#341)
1 parent 01817d2 commit 510f396

File tree

7 files changed

+19
-1238
lines changed

7 files changed

+19
-1238
lines changed

pkgs/web_socket_channel/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- Provide an adapter around `package:web_socket` `WebSocket`s and make it the
44
default implementation for `WebSocketChannel.connect`.
5+
- **BREAKING**: Remove `WebSocketChannel` constructor.
6+
- **BREAKING**: Make `WebSocketChannel` an `abstract interface`.
57

68
## 2.4.5
79

pkgs/web_socket_channel/lib/src/channel.dart

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import 'package:crypto/crypto.dart';
1010
import 'package:stream_channel/stream_channel.dart';
1111

1212
import '../web_socket_adapter_web_socket_channel.dart';
13-
import 'copy/web_socket_impl.dart';
1413
import 'exception.dart';
1514

15+
const String _webSocketGUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
16+
1617
/// A [StreamChannel] that communicates over a WebSocket.
1718
///
1819
/// This is implemented by classes that use `dart:io` and `dart:html`.
@@ -21,33 +22,27 @@ import 'exception.dart';
2122
///
2223
/// All implementations emit [WebSocketChannelException]s. These exceptions wrap
2324
/// 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 {
3126
/// The subprotocol selected by the server.
3227
///
3328
/// For a client socket, this is initially `null`. After the WebSocket
3429
/// connection is established the value is set to the subprotocol selected by
3530
/// the server. If no subprotocol is negotiated the value will remain `null`.
36-
String? get protocol => _webSocket.protocol;
31+
String? get protocol;
3732

3833
/// The [close code][] set when the WebSocket connection is closed.
3934
///
4035
/// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5
4136
///
4237
/// Before the connection has been closed, this will be `null`.
43-
int? get closeCode => _webSocket.closeCode;
38+
int? get closeCode;
4439

4540
/// The [close reason][] set when the WebSocket connection is closed.
4641
///
4742
/// [close reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6
4843
///
4944
/// Before the connection has been closed, this will be `null`.
50-
String? get closeReason => _webSocket.closeReason;
45+
String? get closeReason;
5146

5247
/// A future that will complete when the WebSocket connection has been
5348
/// established.
@@ -74,17 +69,14 @@ class WebSocketChannel extends StreamChannelMixin {
7469
/// // send data.
7570
/// channel.sink.add('Hello World');
7671
/// ```
77-
final Future<void> ready = Future.value();
78-
79-
@override
80-
Stream get stream => StreamView(_webSocket);
72+
Future<void> get ready;
8173

8274
/// The sink for sending values to the other endpoint.
8375
///
8476
/// This supports additional arguments to [WebSocketSink.close] that provide
8577
/// the remote endpoint reasons for closing the connection.
8678
@override
87-
WebSocketSink get sink => WebSocketSink._(_webSocket);
79+
WebSocketSink get sink;
8880

8981
/// Signs a `Sec-WebSocket-Key` header sent by a WebSocket client as part of
9082
/// the [initial handshake][].
@@ -98,33 +90,7 @@ class WebSocketChannel extends StreamChannelMixin {
9890
// [key] is expected to be base64 encoded, and so will be pure ASCII.
9991
=>
10092
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);
12894

12995
/// Creates a new WebSocket connection.
13096
///
@@ -138,19 +104,15 @@ class WebSocketChannel extends StreamChannelMixin {
138104
/// The [ready] future will complete after the channel is connected.
139105
/// If there are errors creating the connection the [ready] future will
140106
/// complete with an error.
141-
factory WebSocketChannel.connect(Uri uri, {Iterable<String>? protocols}) =>
107+
static WebSocketChannel connect(Uri uri, {Iterable<String>? protocols}) =>
142108
WebSocketAdapterWebSocketChannel.connect(uri, protocols: protocols);
143109
}
144110

145111
/// The sink exposed by a [WebSocketChannel].
146112
///
147113
/// This is like a normal [StreamSink], except that it supports extra arguments
148114
/// 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 {
154116
/// Closes the web socket connection.
155117
///
156118
/// [closeCode] and [closeReason] are the [close code][] and [reason][] sent
@@ -160,6 +122,5 @@ class WebSocketSink extends DelegatingStreamSink {
160122
/// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5
161123
/// [reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6
162124
@override
163-
Future close([int? closeCode, String? closeReason]) =>
164-
_webSocket.close(closeCode, closeReason);
125+
Future close([int? closeCode, String? closeReason]);
165126
}

pkgs/web_socket_channel/lib/src/copy/io_sink.dart

Lines changed: 0 additions & 152 deletions
This file was deleted.

pkgs/web_socket_channel/lib/src/copy/web_socket.dart

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)