A cross platform Audiobookshelf client for Android and iOS
Dart
Add Socket.IO connection for online status and last seen
Nathan Poulsonb289fd12e350
diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dartindex f8a7ccbb..cfef6d51 100644--- a/lib/providers/library_provider.dart+++ b/lib/providers/library_provider.dart@@ -12,6 +12,7 @@ import '../services/progress_sync_service.dart';import '../services/download_service.dart';import '../services/android_auto_service.dart';import '../services/chromecast_service.dart';+import '../services/socket_service.dart';import '../main.dart' show scaffoldMessengerKey;/// Holds library data and personalized home sections.@@ -451,6 +452,10 @@ class LibraryProvider extends ChangeNotifier {ProgressSyncService().flushPendingSync(api: _api!);DownloadService().enrichMetadata(_api!);}+ // Connect Socket.IO for online presence+ if (auth.serverUrl != null && auth.token != null) {+ SocketService().connect(auth.serverUrl!, auth.token!);+ }debugPrint('[Library] Calling loadLibraries()');loadLibraries();}).catchError((e) {@@ -473,6 +478,7 @@ class LibraryProvider extends ChangeNotifier {_errorMessage = null;_connectivitySub?.cancel();_stopServerPingTimer();+ SocketService().disconnect();_personalizedInFlight = null;_lastPersonalizedFetchAt = null;_lastPersonalizedFetchLibraryId = null;diff --git a/lib/services/socket_service.dart b/lib/services/socket_service.dartnew file mode 100644index 00000000..cafdc00b--- /dev/null+++ b/lib/services/socket_service.dart@@ -0,0 +1,57 @@+import 'package:flutter/foundation.dart';+import 'package:socket_io_client/socket_io_client.dart' as IO;++class SocketService {+ static final SocketService _instance = SocketService._();+ factory SocketService() => _instance;+ SocketService._();++ IO.Socket? _socket;+ String? _token;++ void connect(String serverUrl, String token) {+ if (_socket != null) disconnect();++ _token = token;++ try {+ _socket = IO.io(serverUrl, IO.OptionBuilder()+ .setTransports(['websocket'])+ .enableReconnection()+ .build());++ // onConnect fires on initial connect AND every reconnect+ _socket!.onConnect((_) {+ debugPrint('[Socket] Connected, sending auth');+ _socket!.emit('auth', _token);+ });++ _socket!.on('init', (_) {+ debugPrint('[Socket] Authenticated - user is online');+ });++ _socket!.on('auth_failed', (_) {+ debugPrint('[Socket] Auth failed');+ disconnect();+ });++ _socket!.onDisconnect((_) {+ debugPrint('[Socket] Disconnected');+ });++ _socket!.onConnectError((err) {+ debugPrint('[Socket] Connect error: $err');+ });+ } catch (e) {+ debugPrint('[Socket] Failed to connect: $e');+ _socket = null;+ _token = null;+ }+ }++ void disconnect() {+ _socket?.dispose();+ _socket = null;+ _token = null;+ }+}diff --git a/pubspec.lock b/pubspec.lockindex 3028881a..37699885 100644--- a/pubspec.lock+++ b/pubspec.lock@@ -335,7 +335,7 @@ packages:source: hostedversion: "7.2.0"flutter_native_splash:- dependency: "direct dev"+ dependency: "direct main"description:name: flutter_native_splashsha256: "4fb9f4113350d3a80841ce05ebf1976a36de622af7d19aca0ca9a9911c7ff002"@@ -844,6 +844,22 @@ packages:description: fluttersource: sdkversion: "0.0.0"+ socket_io_client:+ dependency: "direct main"+ description:+ name: socket_io_client+ sha256: ef6c989e5eee8d04baf18482ec3d7699b91bc41e279794a99d8e3bef897b074a+ url: "https://pub.dev"+ source: hosted+ version: "3.1.4"+ socket_io_common:+ dependency: transitive+ description:+ name: socket_io_common+ sha256: "162fbaecbf4bf9a9372a62a341b3550b51dcef2f02f3e5830a297fd48203d45b"+ url: "https://pub.dev"+ source: hosted+ version: "3.1.1"source_span:dependency: transitivedescription:diff --git a/pubspec.yaml b/pubspec.yamlindex e742fc6f..e03296ab 100644--- a/pubspec.yaml+++ b/pubspec.yaml@@ -12,6 +12,7 @@ dependencies:# Networkinghttp: ^1.1.0+ socket_io_client: ^3.0.2# State Managementprovider: ^6.1.1@@ -71,12 +72,14 @@ dependencies:# Homescreen widgethome_widget: ^0.7.0+ # Splash screen (preserve during init)+ flutter_native_splash: ^2.4.4+dev_dependencies:flutter_test:sdk: flutterflutter_lints: ^3.0.1flutter_launcher_icons: ^0.14.3- flutter_native_splash: ^2.4.4change_app_package_name: ^1.5.0rename_app: ^1.6.5