Skip to content

Commit

Permalink
Merge pull request dart-lang#83 from dart-lang/config_specific_imports
Browse files Browse the repository at this point in the history
Add `WebSocketChannel.connect` factory constructor.
  • Loading branch information
jacob314 authored Oct 11, 2019
2 parents 24382b6 + dbdaf56 commit ba2bae6
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
13 changes: 13 additions & 0 deletions lib/src/_connect_api.dart
Original file line number Diff line number Diff line change
@@ -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';

/// Creates a new WebSocket connection.
///
/// Connects to [uri] using and returns a channel that can be used to
/// communicate over the resulting socket.
WebSocketChannel connect(Uri uri) {
throw UnsupportedError('No implementation of the connect api provided');
}
13 changes: 13 additions & 0 deletions lib/src/_connect_html.dart
Original file line number Diff line number Diff line change
@@ -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 'package:web_socket_channel/html.dart';

import '../web_socket_channel.dart';

/// Creates a new WebSocket connection.
///
/// Connects to [uri] using and returns a channel that can be used to
/// communicate over the resulting socket.
WebSocketChannel connect(Uri uri) => HtmlWebSocketChannel.connect(uri);
12 changes: 12 additions & 0 deletions lib/src/_connect_io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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 [uri] using and returns a channel that can be used to
/// communicate over the resulting socket
WebSocketChannel connect(Uri uri) => IOWebSocketChannel.connect(uri);
10 changes: 10 additions & 0 deletions lib/src/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import 'package:stream_channel/stream_channel.dart';

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
Expand Down Expand Up @@ -94,6 +98,12 @@ class WebSocketChannel extends StreamChannelMixin {
: _webSocket = WebSocketImpl.fromSocket(
channel.stream, channel.sink, protocol, serverSide)
..pingInterval = pingInterval;

/// Creates a new WebSocket connection.
///
/// Connects to [uri] using and returns a channel that can be used to
/// communicate over the resulting socket.
factory WebSocketChannel.connect(Uri uri) => platform.connect(uri);
}

/// The sink exposed by a [WebSocketChannel].
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/html_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ void main() {
expect(await queue.next, equals([1, 2, 3, 4, 5]));
});

test(".connect defaults to binary lists using platform independent api",
() async {
channel = WebSocketChannel.connect(Uri.parse("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);
Expand Down
23 changes: 23 additions & 0 deletions test/io_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,29 @@ void main() {
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(Uri.parse("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) {
Expand Down

0 comments on commit ba2bae6

Please sign in to comment.