Skip to content

Commit

Permalink
Rename WebSocketAdapterWebSocketChannel to AdapterWebSocketChannel (#344
Browse files Browse the repository at this point in the history
)
  • Loading branch information
brianquinlan authored Apr 9, 2024
1 parent 429e7d9 commit ce69d31
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'src/channel.dart';
import 'src/exception.dart';

/// A [WebSocketChannel] implemented using [WebSocket].
class WebSocketAdapterWebSocketChannel extends StreamChannelMixin
class AdapterWebSocketChannel extends StreamChannelMixin
implements WebSocketChannel {
@override
String? get protocol => _protocol;
Expand Down Expand Up @@ -60,18 +60,17 @@ class WebSocketAdapterWebSocketChannel extends StreamChannelMixin
/// the peer is able to select. See
/// [RFC-6455 1.9](https://datatracker.ietf.org/doc/html/rfc6455#section-1.9).
///
/// After construction, the [WebSocketAdapterWebSocketChannel] may not be
/// After construction, the [AdapterWebSocketChannel] may not be
/// connected to the peer. The [ready] future will complete after the channel
/// is connected. If there are errors creating the connection the [ready]
/// future will complete with an error.
factory WebSocketAdapterWebSocketChannel.connect(Uri url,
factory AdapterWebSocketChannel.connect(Uri url,
{Iterable<String>? protocols}) =>
WebSocketAdapterWebSocketChannel(
WebSocket.connect(url, protocols: protocols));
AdapterWebSocketChannel(WebSocket.connect(url, protocols: protocols));

// Construct a [WebSocketWebSocketChannelAdapter] from an existing
// [WebSocket].
WebSocketAdapterWebSocketChannel(FutureOr<WebSocket> webSocket) {
AdapterWebSocketChannel(FutureOr<WebSocket> webSocket) {
Future<WebSocket> webSocketFuture;
if (webSocket is WebSocket) {
webSocketFuture = Future.value(webSocket);
Expand Down Expand Up @@ -135,9 +134,9 @@ class WebSocketAdapterWebSocketChannel extends StreamChannelMixin
/// A [WebSocketSink] that tracks the close code and reason passed to [close].
class _WebSocketSink extends DelegatingStreamSink implements WebSocketSink {
/// The channel to which this sink belongs.
final WebSocketAdapterWebSocketChannel _channel;
final AdapterWebSocketChannel _channel;

_WebSocketSink(WebSocketAdapterWebSocketChannel channel)
_WebSocketSink(AdapterWebSocketChannel channel)
: _channel = channel,
super(channel._controller.foreign.sink);

Expand Down
5 changes: 3 additions & 2 deletions lib/io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

import 'dart:async';
import 'dart:io' show HttpClient, WebSocket;

import 'package:web_socket/io_web_socket.dart' as io_web_socket;

import 'adapter_web_socket_channel.dart';
import 'src/channel.dart';
import 'src/exception.dart';
import 'web_socket_adapter_web_socket_channel.dart';

/// A [WebSocketChannel] that communicates using a `dart:io` [WebSocket].
class IOWebSocketChannel extends WebSocketAdapterWebSocketChannel {
class IOWebSocketChannel extends AdapterWebSocketChannel {
/// Creates a new WebSocket connection.
///
/// Connects to [url] using [WebSocket.connect] and returns a channel that can
Expand Down
4 changes: 2 additions & 2 deletions lib/src/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:async/async.dart';
import 'package:crypto/crypto.dart';
import 'package:stream_channel/stream_channel.dart';

import '../web_socket_adapter_web_socket_channel.dart';
import '../adapter_web_socket_channel.dart';
import 'exception.dart';

const String _webSocketGUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
Expand Down Expand Up @@ -105,7 +105,7 @@ abstract interface class WebSocketChannel extends StreamChannelMixin {
/// If there are errors creating the connection the [ready] future will
/// complete with an error.
static WebSocketChannel connect(Uri uri, {Iterable<String>? protocols}) =>
WebSocketAdapterWebSocketChannel.connect(uri, protocols: protocols);
AdapterWebSocketChannel.connect(uri, protocols: protocols);
}

/// The sink exposed by a [WebSocketChannel].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import 'package:async/async.dart';
import 'package:stream_channel/stream_channel.dart';
import 'package:test/test.dart';
import 'package:web_socket/web_socket.dart';
import 'package:web_socket_channel/adapter_web_socket_channel.dart';
import 'package:web_socket_channel/src/exception.dart';
import 'package:web_socket_channel/web_socket_adapter_web_socket_channel.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

import 'echo_server_vm.dart'
if (dart.library.js_interop) 'echo_server_web.dart';

void main() {
group('WebSocketWebSocketChannelAdapter', () {
group('AdapterWebSocketChannel', () {
late Uri uri;
late StreamChannel<Object?> httpServerChannel;
late StreamQueue<Object?> httpServerQueue;
Expand All @@ -34,52 +35,52 @@ void main() {

test('failed connect', () async {
final channel =
WebSocketAdapterWebSocketChannel.connect(Uri.parse('ws://notahost'));
AdapterWebSocketChannel.connect(Uri.parse('ws://notahost'));

await expectLater(
channel.ready, throwsA(isA<WebSocketChannelException>()));
});

test('good connect', () async {
final channel = WebSocketAdapterWebSocketChannel.connect(uri);
final channel = AdapterWebSocketChannel.connect(uri);
await expectLater(channel.ready, completes);
await channel.sink.close();
});

test('echo empty text', () async {
final channel = WebSocketAdapterWebSocketChannel.connect(uri);
final channel = AdapterWebSocketChannel.connect(uri);
await expectLater(channel.ready, completes);
channel.sink.add('');
expect(await channel.stream.first, '');
await channel.sink.close();
});

test('echo empty binary', () async {
final channel = WebSocketAdapterWebSocketChannel.connect(uri);
final channel = AdapterWebSocketChannel.connect(uri);
await expectLater(channel.ready, completes);
channel.sink.add(Uint8List.fromList(<int>[]));
expect(await channel.stream.first, isEmpty);
await channel.sink.close();
});

test('echo hello', () async {
final channel = WebSocketAdapterWebSocketChannel.connect(uri);
final channel = AdapterWebSocketChannel.connect(uri);
await expectLater(channel.ready, completes);
channel.sink.add('hello');
expect(await channel.stream.first, 'hello');
await channel.sink.close();
});

test('echo [1,2,3]', () async {
final channel = WebSocketAdapterWebSocketChannel.connect(uri);
final channel = AdapterWebSocketChannel.connect(uri);
await expectLater(channel.ready, completes);
channel.sink.add([1, 2, 3]);
expect(await channel.stream.first, [1, 2, 3]);
await channel.sink.close();
});

test('alternative string and binary request and response', () async {
final channel = WebSocketAdapterWebSocketChannel.connect(uri);
final channel = AdapterWebSocketChannel.connect(uri);
await expectLater(channel.ready, completes);
channel.sink.add('This count says:');
channel.sink.add([1, 2, 3]);
Expand All @@ -94,7 +95,7 @@ void main() {
});

test('remote close', () async {
final channel = WebSocketAdapterWebSocketChannel.connect(uri);
final channel = AdapterWebSocketChannel.connect(uri);
await expectLater(channel.ready, completes);
channel.sink.add('close'); // Asks the peer to close.
// Give the server time to send a close frame.
Expand All @@ -105,7 +106,7 @@ void main() {
});

test('local close', () async {
final channel = WebSocketAdapterWebSocketChannel.connect(uri);
final channel = AdapterWebSocketChannel.connect(uri);
await expectLater(channel.ready, completes);
await channel.sink.close(3005, 'please close');
expect(channel.closeCode, null);
Expand All @@ -114,7 +115,7 @@ void main() {

test('constructor with WebSocket', () async {
final webSocket = await WebSocket.connect(uri);
final channel = WebSocketAdapterWebSocketChannel(webSocket);
final channel = AdapterWebSocketChannel(webSocket);

await expectLater(channel.ready, completes);
channel.sink.add('This count says:');
Expand All @@ -131,7 +132,7 @@ void main() {

test('constructor with Future<WebSocket>', () async {
final webSocketFuture = WebSocket.connect(uri);
final channel = WebSocketAdapterWebSocketChannel(webSocketFuture);
final channel = AdapterWebSocketChannel(webSocketFuture);

await expectLater(channel.ready, completes);
channel.sink.add('This count says:');
Expand Down

0 comments on commit ce69d31

Please sign in to comment.