Skip to content

Commit

Permalink
migrate to pkg:web
Browse files Browse the repository at this point in the history
  • Loading branch information
mytja committed Dec 22, 2023
1 parent a082f5c commit 3436cd9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 2 additions & 3 deletions lib/src/_web_socket_channel/_web_socket_channel_html.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import 'dart:html' as html;

import 'package:web/web.dart';
import 'package:web_socket_channel/html.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

/// Get am [HtmlWebSocketChannel] for the provided [socket].
WebSocketChannel getWebSocketChannel(html.WebSocket socket) {
WebSocketChannel getWebSocketChannel(WebSocket socket) {
return HtmlWebSocketChannel(socket);
}
21 changes: 19 additions & 2 deletions lib/src/_web_socket_connect/_web_socket_connect_html.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import 'dart:async';
import 'dart:html';
import 'dart:js_interop';

import 'package:web/helpers.dart';

// TODO: remove when https://github.com/dart-lang/web/commit/4cb5811ed06
// is in a published release and the min constraint on pkg:web is updated
extension WebSocketEvents on WebSocket {
Stream<Event> get onOpen => EventStreamProviders.openEvent.forTarget(this);
Stream<MessageEvent> get onMessage =>
EventStreamProviders.messageEvent.forTarget(this);
Stream<CloseEvent> get onClose =>
EventStreamProviders.closeEvent.forTarget(this);
Stream<Event> get onError =>
EventStreamProviders.errorEventSourceEvent.forTarget(this);
}

/// Create a WebSocket connection.
Future<WebSocket> connect(
Expand All @@ -8,7 +22,10 @@ Future<WebSocket> connect(
Duration? pingInterval,
String? binaryType,
}) async {
final socket = WebSocket(url, protocols)..binaryType = binaryType;
final socket = WebSocket(
url,
protocols?.map((e) => e.toJS).toList().toJS ?? JSArray(),
)..binaryType = binaryType ?? 'list';

if (socket.readyState == 1) return socket;

Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ environment:
sdk: ">=2.19.0 <4.0.0"

dependencies:
web: ^0.4.0
web_socket_channel: ^2.0.0

dev_dependencies:
Expand Down

0 comments on commit 3436cd9

Please sign in to comment.