Add `WebSocketChannel.connect` factory constructor. This supports platform independent creation of WebSockets providing the lowest common denominator of support on dart:io and dart:html.
diff --git a/pkgs/web_socket_channel/CHANGELOG.md b/pkgs/web_socket_channel/CHANGELOG.md index dbd834f..ec82a91 100644 --- a/pkgs/web_socket_channel/CHANGELOG.md +++ b/pkgs/web_socket_channel/CHANGELOG.md
@@ -1,3 +1,9 @@ +## 1.1.0 + +* Add `WebSocketChannel.connect` factory constructor supporting platform + independent creation of WebSockets providing the lowest common denominator + of support on dart:io and dart:html. + ## 1.0.15 * bug fix don't pass protocols parameter to WebSocket.
diff --git a/pkgs/web_socket_channel/lib/src/_connect_api.dart b/pkgs/web_socket_channel/lib/src/_connect_api.dart new file mode 100644 index 0000000..13d22eb --- /dev/null +++ b/pkgs/web_socket_channel/lib/src/_connect_api.dart
@@ -0,0 +1,14 @@ +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import '../web_socket_channel.dart'; + +/// Creates a new WebSocket connection. +/// +/// Connects to [url] using and returns a channel that can be used to +/// communicate over the resulting socket. The [url] may be either a [String] +/// or a [Uri]. +WebSocketChannel connect(String url) { + throw UnsupportedError('No implementation of the connect api provided'); +}
diff --git a/pkgs/web_socket_channel/lib/src/_connect_html.dart b/pkgs/web_socket_channel/lib/src/_connect_html.dart new file mode 100644 index 0000000..d8e5ec7 --- /dev/null +++ b/pkgs/web_socket_channel/lib/src/_connect_html.dart
@@ -0,0 +1,14 @@ +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:web_socket_channel/html.dart'; + +import '../web_socket_channel.dart'; + +/// Creates a new WebSocket connection. +/// +/// Connects to [url] using and returns a channel that can be used to +/// communicate over the resulting socket. The [url] may be either a [String] +/// or a [Uri]. +WebSocketChannel connect(url) => HtmlWebSocketChannel.connect(url);
diff --git a/pkgs/web_socket_channel/lib/src/_connect_io.dart b/pkgs/web_socket_channel/lib/src/_connect_io.dart new file mode 100644 index 0000000..80d362d --- /dev/null +++ b/pkgs/web_socket_channel/lib/src/_connect_io.dart
@@ -0,0 +1,13 @@ +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import '../web_socket_channel.dart'; +import '../io.dart'; + +/// Creates a new WebSocket connection. +/// +/// Connects to [url] using and returns a channel that can be used to +/// communicate over the resulting socket. The [url] may be either a [String] +/// or a [Uri]. +WebSocketChannel connect(url) => IOWebSocketChannel.connect(url);
diff --git a/pkgs/web_socket_channel/lib/src/channel.dart b/pkgs/web_socket_channel/lib/src/channel.dart index bbdaf34..6bbb3c4 100644 --- a/pkgs/web_socket_channel/lib/src/channel.dart +++ b/pkgs/web_socket_channel/lib/src/channel.dart
@@ -11,6 +11,10 @@ import 'copy/web_socket_impl.dart'; +import '_connect_api.dart' + if (dart.library.io) '_connect_io.dart' + if (dart.library.html) '_connect_html.dart' as platform; + /// A [StreamChannel] that communicates over a WebSocket. /// /// This is implemented by classes that use `dart:io` and `dart:html`. The [new @@ -94,6 +98,13 @@ : _webSocket = WebSocketImpl.fromSocket( channel.stream, channel.sink, protocol, serverSide) ..pingInterval = pingInterval; + + /// Creates a new WebSocket connection. + /// + /// Connects to [url] using and returns a channel that can be used to + /// communicate over the resulting socket. The [url] may be either a [String] + /// or a [Uri]. + factory WebSocketChannel.connect(url) => platform.connect(url); } /// The sink exposed by a [WebSocketChannel].
diff --git a/pkgs/web_socket_channel/pubspec.yaml b/pkgs/web_socket_channel/pubspec.yaml index 71d8c19..e5a2b35 100644 --- a/pkgs/web_socket_channel/pubspec.yaml +++ b/pkgs/web_socket_channel/pubspec.yaml
@@ -1,5 +1,5 @@ name: web_socket_channel -version: 1.0.15 +version: 1.1.0 description: >- StreamChannel wrappers for WebSockets. Provides a cross-platform
diff --git a/pkgs/web_socket_channel/test/html_test.dart b/pkgs/web_socket_channel/test/html_test.dart index fac1e91..75aacfb 100644 --- a/pkgs/web_socket_channel/test/html_test.dart +++ b/pkgs/web_socket_channel/test/html_test.dart
@@ -79,6 +79,18 @@ expect(await queue.next, equals([1, 2, 3, 4, 5])); }); + test(".connect defaults to binary lists using platform independent api", + () async { + channel = WebSocketChannel.connect("ws://localhost:$port"); + + var queue = StreamQueue(channel.stream); + channel.sink.add("foo"); + expect(await queue.next, equals("foo")); + + channel.sink.add(Uint8List.fromList([1, 2, 3, 4, 5])); + expect(await queue.next, equals([1, 2, 3, 4, 5])); + }); + test(".connect can use blobs", () async { channel = HtmlWebSocketChannel.connect("ws://localhost:$port", binaryType: BinaryType.blob);
diff --git a/pkgs/web_socket_channel/test/io_test.dart b/pkgs/web_socket_channel/test/io_test.dart index f9032cd..b389699 100644 --- a/pkgs/web_socket_channel/test/io_test.dart +++ b/pkgs/web_socket_channel/test/io_test.dart
@@ -70,6 +70,28 @@ onDone: expectAsync0(() {})); }); + test(".connect communicates immediately using platform independent api", + () async { + server = await HttpServer.bind("localhost", 0); + server.transform(WebSocketTransformer()).listen((webSocket) { + var channel = IOWebSocketChannel(webSocket); + channel.stream.listen((request) { + expect(request, equals("ping")); + channel.sink.add("pong"); + }); + }); + + var channel = WebSocketChannel.connect("ws://localhost:${server.port}"); + channel.sink.add("ping"); + + channel.stream.listen( + expectAsync1((message) { + expect(message, equals("pong")); + channel.sink.close(5678, "raisin"); + }, count: 1), + onDone: expectAsync0(() {})); + }); + test(".connect with an immediate call to close", () async { server = await HttpServer.bind("localhost", 0); server.transform(WebSocketTransformer()).listen((webSocket) {