diff --git a/lib/status_page.dart b/lib/status_page.dart index 5c13614..4109391 100644 --- a/lib/status_page.dart +++ b/lib/status_page.dart @@ -1,4 +1,3 @@ -import 'dart:async'; import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -12,17 +11,13 @@ import 'custom_menu.dart'; class StatusPage extends StatefulWidget { final VoidCallback toggleProfile; - const StatusPage({super.key, required this.toggleProfile}); - @override StatusPageState createState() => StatusPageState(); } class StatusPageState extends State with WidgetsBindingObserver { - WebSocketChannel? channel; - late final Stream broadcastStream; - late StreamController controller; + late final WebSocketChannel channel; List> messages = []; final Map _imageCache = {}; static const wsBaseUrl = String.fromEnvironment('WS_BASE_URL', @@ -41,17 +36,14 @@ class StatusPageState extends State with WidgetsBindingObserver { channel = WebSocketChannel.connect( Uri.parse('$wsBaseUrl/ws'), ); - - controller = StreamController.broadcast(); - controller.addStream(channel!.stream); - broadcastStream = channel!.stream.asBroadcastStream(); + debugPrint("WebSocket initialized at: $wsBaseUrl/ws"); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); - channel?.sink.close(); - controller.close(); + channel.sink.close(); + debugPrint("WebSocket connection closed."); super.dispose(); } @@ -63,8 +55,8 @@ class StatusPageState extends State with WidgetsBindingObserver { } void _reconnectWebSocket() { - if (channel == null || channel!.closeCode != null) { - // Check if channel is null or already closed + if (channel.closeCode != null) { + debugPrint("Reinitializing WebSocket connection..."); _initializeWebSocket(); } } @@ -310,7 +302,7 @@ class StatusPageState extends State with WidgetsBindingObserver { ), ), body: StreamBuilder( - stream: controller.stream, + stream: channel.stream, builder: (context, snapshot) { if (snapshot.hasData) { final newMessage =