Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WebSocket.connect as a cross-platform connection method #1149

Merged
merged 4 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pkgs/web_socket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ implementations.
## Using

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

void main() async {
final socket =
await IOWebSocket.connect(Uri.parse('wss://ws.postman-echo.com/raw'));
await WebSocket.connect(Uri.parse('wss://ws.postman-echo.com/raw'));

socket.events.listen((e) async {
switch (e) {
Expand Down
3 changes: 1 addition & 2 deletions pkgs/web_socket/example/web_socket_example.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:convert';
import 'dart:io';

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

const requestId = 305;
Expand All @@ -11,7 +10,7 @@ void main() async {
// Whitebit public WebSocket API documentation:
// https://docs.whitebit.com/public/websocket/
final socket =
await IOWebSocket.connect(Uri.parse('wss://api.whitebit.com/ws'));
await WebSocket.connect(Uri.parse('wss://api.whitebit.com/ws'));

socket.events.listen((e) {
switch (e) {
Expand Down
5 changes: 4 additions & 1 deletion pkgs/web_socket/lib/src/browser_web_socket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class BrowserWebSocket implements WebSocket {
final web.WebSocket _webSocket;
final _events = StreamController<WebSocketEvent>();

static Future<BrowserWebSocket> connect(Uri url) async {
static Future<BrowserWebSocket> connect(Uri url,
{Iterable<String>? protocols}) async {
final webSocket = web.WebSocket(url.toString())..binaryType = 'arraybuffer';
final browserSocket = BrowserWebSocket._(webSocket);
final webSocketConnected = Completer<BrowserWebSocket>();
Expand Down Expand Up @@ -126,3 +127,5 @@ class BrowserWebSocket implements WebSocket {
@override
Stream<WebSocketEvent> get events => _events.stream;
}

const connect = BrowserWebSocket.connect;
9 changes: 9 additions & 0 deletions pkgs/web_socket/lib/src/connect_stub.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2024, 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.dart';

Future<WebSocket> connect(Uri url, {Iterable<String>? protocols}) {
throw UnsupportedError('Cannot connect without dart:js_interop or dart:io.');
}
7 changes: 5 additions & 2 deletions pkgs/web_socket/lib/src/io_web_socket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ class IOWebSocket implements WebSocket {
final io.WebSocket _webSocket;
final _events = StreamController<WebSocketEvent>();

static Future<IOWebSocket> connect(Uri uri) async {
static Future<IOWebSocket> connect(Uri url,
{Iterable<String>? protocols}) async {
try {
final webSocket = await io.WebSocket.connect(uri.toString());
final webSocket = await io.WebSocket.connect(url.toString());
return IOWebSocket._(webSocket);
} on io.WebSocketException catch (e) {
throw WebSocketException(e.message);
Expand Down Expand Up @@ -90,3 +91,5 @@ class IOWebSocket implements WebSocket {
@override
Stream<WebSocketEvent> get events => _events.stream;
}

const connect = IOWebSocket.connect;
10 changes: 8 additions & 2 deletions pkgs/web_socket/lib/src/web_socket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

import 'dart:typed_data';

import 'connect_stub.dart'
if (dart.library.js_interop) 'browser_web_socket.dart'
if (dart.library.io) 'io_web_socket.dart' as connector;

/// An event received from the peer through the [WebSocket].
sealed class WebSocketEvent {}

Expand Down Expand Up @@ -90,12 +94,11 @@ class WebSocketConnectionClosed extends WebSocketException {
/// The interface for WebSocket connections.
///
/// ```dart
/// import 'package:web_socket/io_web_socket.dart';
/// import 'package:web_socket/src/web_socket.dart';
///
/// void main() async {
/// final socket =
/// await IOWebSocket.connect(Uri.parse('wss://ws.postman-echo.com/raw'));
/// await WebSocket.connect(Uri.parse('wss://ws.postman-echo.com/raw'));
///
/// socket.events.listen((e) async {
/// switch (e) {
Expand All @@ -112,6 +115,9 @@ class WebSocketConnectionClosed extends WebSocketException {
/// socket.sendText('Hello Dart WebSockets! 🎉');
/// }
abstract interface class WebSocket {
static Future<WebSocket> connect(Uri url, {Iterable<String>? protocols}) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a doc comment? Can you describe the behavior of the protocols argument, and consider including the information that it is ignored on the web?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a follow-up PR that enables the protocol functionality and adds comments: #1150

I wanted to add a cross-platform connect in this PR but realized that I had a non-function protocol argument in the tests but not in connect. So I wanted to fix that without implementing the functionality in the same PR - it felt like too much for one commit.

connector.connect(url, protocols: protocols);

/// Sends text data to the connected peer.
///
/// Throws [WebSocketConnectionClosed] if the [WebSocket] is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import 'package:web_socket/browser_web_socket.dart';
import 'package:web_socket_conformance_tests/web_socket_conformance_tests.dart';

void main() {
testAll((uri, {protocols}) => BrowserWebSocket.connect(uri));
testAll(BrowserWebSocket.connect);
}
2 changes: 1 addition & 1 deletion pkgs/web_socket/test/io_web_socket_conformance_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import 'package:web_socket/io_web_socket.dart';
import 'package:web_socket_conformance_tests/web_socket_conformance_tests.dart';

void main() {
testAll((uri, {protocols}) => IOWebSocket.connect(uri));
testAll(IOWebSocket.connect);
}
10 changes: 10 additions & 0 deletions pkgs/web_socket/test/websocket_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2024, 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/web_socket.dart';
import 'package:web_socket_conformance_tests/web_socket_conformance_tests.dart';

void main() {
testAll(WebSocket.connect);
}