Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Send custom headers on socket.io connections (#148)

Nathan Poulsonf954aae610c2
diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dart
index 40212705..14c37477 100644
--- a/lib/providers/library_provider.dart
+++ b/lib/providers/library_provider.dart
@@ -129,7 +129,7 @@ class LibraryProvider extends ChangeNotifier
socket.onCollectionUpdated = _onRemoteCollectionUpdated;
socket.onUserUpdated = _onRemoteUserUpdated;
socket.onReconnectFailed = _onSocketReconnectFailed;
- socket.connect(auth.serverUrl!, auth.token!);
+ socket.connect(auth.serverUrl!, auth.token!, customHeaders: auth.customHeaders);
}
debugPrint('[Library] Calling loadLibraries()');
loadLibraries();
diff --git a/lib/services/socket_service.dart b/lib/services/socket_service.dart
index e534efe4..aa9945c8 100644
--- a/lib/services/socket_service.dart
+++ b/lib/services/socket_service.dart
@@ -14,15 +14,22 @@ class SocketService {
bool get isConnected => _socket?.connected ?? false;
bool get hasSocket => _socket != null;
+ Map<String, String> _customHeaders = {};
+
/// Build socket.io options with capped reconnection to avoid
/// hammering an unreachable server (and draining battery).
- Map<String, dynamic> _buildOptions() => io.OptionBuilder()
- .setTransports(['websocket'])
- .enableReconnection()
- .setReconnectionDelay(1000)
- .setReconnectionDelayMax(30000)
- .setReconnectionAttempts(5)
- .build();
+ Map<String, dynamic> _buildOptions() {
+ final builder = io.OptionBuilder()
+ .setTransports(['websocket'])
+ .enableReconnection()
+ .setReconnectionDelay(1000)
+ .setReconnectionDelayMax(30000)
+ .setReconnectionAttempts(5);
+ if (_customHeaders.isNotEmpty) {
+ builder.setExtraHeaders(_customHeaders);
+ }
+ return builder.build();
+ }
/// Called when the server pushes a progress update (cross-device sync).
void Function(Map<String, dynamic> progress)? onProgressUpdated;
@@ -54,11 +61,12 @@ class SocketService {
}
}
- void connect(String serverUrl, String token) {
+ void connect(String serverUrl, String token, {Map<String, String> customHeaders = const {}}) {
if (_socket != null) disconnect();
_token = token;
_serverUrl = serverUrl;
+ _customHeaders = customHeaders;
try {
_socket = io.io(serverUrl, _buildOptions());