Skip to content
New issue

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

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

Already on GitHub? # to your account

Web support #64

Merged
merged 4 commits into from
Mar 18, 2023
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
25 changes: 11 additions & 14 deletions lib/src/ddp_client.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:convert';
import 'dart:io';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'dart:async';
import 'dart:math';

Expand Down Expand Up @@ -89,7 +89,7 @@ class DdpClient {
late DdpConnectionStatus _connectionStatus;
String url;
String userAgent;
WebSocket? _socket;
WebSocketChannel? _socket;
int maxRetryCount;
final Map<String, OnReconnectionCallback> _onReconnectCallbacks = {};
String? serverId;
Expand Down Expand Up @@ -198,7 +198,7 @@ class DdpClient {
printDebug('Begin of disconnect()');
_isTryToReconnect = false;
if (_socket != null) {
_socket!.close().then((value) {
_socket!.sink.close().then((value) {
_socket = null;
}).catchError((err) {
printDebug(err);
Expand Down Expand Up @@ -232,13 +232,10 @@ class DdpClient {
_connectionStatus.reason = null;
_statusStreamController.sink.add(_connectionStatus);
try {
WebSocket.userAgent = userAgent;
_socket = await WebSocket.connect(url).timeout(
Duration(seconds: 5),
);
_socket = WebSocketChannel.connect(Uri.parse(url));
_connectionStatus.retryCount = 0;
_connectionStatus.retryTime = Duration(seconds: 1);
_socket!.listen(
_socket!.stream.listen(
_onData,
onDone: _onDone,
onError: _onError,
Expand Down Expand Up @@ -304,7 +301,7 @@ class DdpClient {
}
var msg = json.encode(data);
printDebug('Send: $msg');
_socket!.add(msg);
_socket!.sink.add(msg);

// Resend all subscriptions
_subscriptionHandlers.forEach((id, handler) {
Expand All @@ -317,7 +314,7 @@ class DdpClient {
if (_socket != null) {
var msg = json.encode({'msg': 'ping'});
printDebug('Send: $msg');
_socket!.add(msg);
_socket!.sink.add(msg);
var sentTime = DateTime.now();
_flagToBeResetAtPongMsg = true;
Future.delayed(Duration(seconds: PONG_WITHIN_SEC), () {
Expand All @@ -339,7 +336,7 @@ class DdpClient {
if (_socket != null) {
var msg = json.encode({'msg': 'pong'});
printDebug('Send: $msg');
_socket!.add(msg);
_socket!.sink.add(msg);
}
}

Expand All @@ -353,7 +350,7 @@ class DdpClient {
};
var msg = json.encode(data);
printDebug('Send: $msg');
_socket!.add(msg);
_socket!.sink.add(msg);
}
}

Expand All @@ -365,7 +362,7 @@ class DdpClient {
};
var msg = json.encode(data);
printDebug('Send: $msg');
_socket!.add(msg);
_socket!.sink.add(msg);
}
}

Expand All @@ -383,7 +380,7 @@ class DdpClient {
}
var msg = json.encode(data);
printDebug('Send: $msg');
_socket!.add(msg);
_socket!.sink.add(msg);
}
}

Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ environment:
dependencies:
crypto: ^3.0.1
rxdart: ^0.27.0
web_socket_channel: ^2.3.0

dev_dependencies:
pedantic: ^1.11.0
Expand Down