Skip to content

Commit

Permalink
feat: add httpClient parameter to WebSocket class
Browse files Browse the repository at this point in the history
Consuming code can now pass in a predefined HttpClient, allowing for customisations such as a SecurityContext with an mTLS client certificate and password.
  • Loading branch information
edkennard committed Aug 24, 2023
1 parent a082f5c commit 60f8472
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
.packages
build/
pubspec.lock
coverage/
coverage/

.idea/
*.iml
3 changes: 3 additions & 0 deletions lib/src/_web_socket_connect/_web_socket_connect.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'dart:io';

/// Create a WebSocket connection.
Future<Stream<dynamic>> connect(
String url, {
Iterable<String>? protocols,
Duration? pingInterval,
String? binaryType,
HttpClient? httpClient,
}) {
throw UnsupportedError('No implementation of the api provided');
}
8 changes: 6 additions & 2 deletions lib/src/_web_socket_connect/_web_socket_connect_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ Future<WebSocket> connect(
Iterable<String>? protocols,
Duration? pingInterval,
String? binaryType,
HttpClient? httpClient,
}) async {
return await WebSocket.connect(url, protocols: protocols)
..pingInterval = pingInterval;
return await WebSocket.connect(
url,
protocols: protocols,
customClient: httpClient,
)..pingInterval = pingInterval;
}
7 changes: 6 additions & 1 deletion lib/src/web_socket.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';

import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:web_socket_client/src/_web_socket_channel/_web_socket_channel.dart'
Expand Down Expand Up @@ -31,12 +32,14 @@ class WebSocket {
Backoff? backoff,
Duration? timeout,
String? binaryType,
HttpClient? httpClient,
}) : _uri = uri,
_protocols = protocols,
_pingInterval = pingInterval,
_backoff = backoff ?? _defaultBackoff,
_timeout = timeout ?? _defaultTimeout,
_binaryType = binaryType {
_binaryType = binaryType,
_httpClient = httpClient {
_connect();
}

Expand All @@ -46,6 +49,7 @@ class WebSocket {
final Backoff _backoff;
final Duration _timeout;
final String? _binaryType;
final HttpClient? _httpClient;

final _messageController = StreamController<dynamic>.broadcast();
final _connectionController = ConnectionController();
Expand Down Expand Up @@ -94,6 +98,7 @@ class WebSocket {
protocols: _protocols,
pingInterval: _pingInterval,
binaryType: _binaryType,
httpClient: _httpClient,
).timeout(_timeout);

final connectionState = _connectionController.state;
Expand Down

0 comments on commit 60f8472

Please sign in to comment.