test(client): first integration test websocket
MODIFY
packages/graphql/lib/src/socket_client.dart
packages/graphql/lib/src/socket_client.dart
import 'dart:async';
import 'dart:convert';
-import 'dart:io';
+import 'package:websocket/websocket.dart' show WebSocket, WebSocketStatus;
import 'package:rxdart/rxdart.dart';
import 'package:uuid/uuid.dart';
@@ -13,7 +13,6 @@ class SocketClientConfig {
this.queryAndMutationTimeout = const Duration(seconds: 10),
this.inactivityTimeout = const Duration(seconds: 30),
this.delayBetweenReconnectionAttempts = const Duration(seconds: 5),
- this.compression = CompressionOptions.compressionDefault,
this.initPayload,
// @todo please review this ignore rule
// ignore: deprecated_member_use_from_same_package
@@ -40,8 +39,6 @@ class SocketClientConfig {
// If null, no timeout is applied, although not recommended.
final Duration queryAndMutationTimeout;
- final CompressionOptions compression;
-
/// The initial payload that will be sent to the server upon connection.
/// Can be null, but must be a valid json structure if provided.
final dynamic initPayload;
@@ -118,12 +115,13 @@ class SocketClient {
_socket = await WebSocket.connect(url,
protocols: protocols,
headers: headers,
- compression: config.compression);
+ );
_connectionStateController.value = SocketConnectionState.CONNECTED;
print('Connected to websocket.');
_write(config.initOperation);
_messageStream = _socket
+ .stream
.asBroadcastStream()
.map<GraphQLSocketMessage>(_parseSocketMessage);
@@ -154,11 +152,14 @@ class SocketClient {
print('error: $e');
});
} catch (e) {
- onConnectionLost();
+ onConnectionLost(e);
}
}
- void onConnectionLost() {
+ void onConnectionLost([e]) {
+ if(e != null) {
+ print('There was an error causing connection lost: $e');
+ }
print('Disconnected from websocket.');
_reconnectTimer?.cancel();
_keepAliveSubscription?.cancel();
@@ -199,10 +200,12 @@ class SocketClient {
Future<void> dispose() async {
print('Disposing socket client..');
_reconnectTimer?.cancel();
- await _socket?.close();
- await _keepAliveSubscription?.cancel();
- await _messageSubscription?.cancel();
- await _connectionStateController?.close();
+ await Future.wait([
+ _socket?.close(),
+ _keepAliveSubscription?.cancel(),
+ _messageSubscription?.cancel(),
+ _connectionStateController?.close(),
+ ]);
}
static GraphQLSocketMessage _parseSocketMessage(dynamic message) {

MODIFY
packages/graphql/pubspec.yaml
packages/graphql/pubspec.yaml
@@ -17,6 +17,7 @@ dependencies:
uuid: ^2.0.1
graphql_parser: ^1.1.3
rxdart: ^0.22.0
+ websocket: ^0.0.2
dev_dependencies:
pedantic: <=1.7.99
mockito: ^4.0.0

ADD
packages/graphql/test/socket_client_test.dart
+import 'package:test/test.dart';
+// import 'dart:html' show WebSocket, querySelector;
+import 'package:graphql/src/socket_client.dart'
+ show SocketClient, SocketConnectionState;
+
+void main() {
+ group('SocketClient', () {
+ test('connection', () async {
+ final c = SocketClient('ws://echo.websocket.org');
+ await expectLater(
+ c.connectionState.asBroadcastStream(),
+ emitsInOrder(
+ [
+ SocketConnectionState.CONNECTING,
+ SocketConnectionState.CONNECTED,
+ ],
+ ),
+ );
+ await c.dispose();
+ }, tags: "integration");
+ });
+}

test(client): first integration test websocket