removing broadcastStream
Some checks failed
Build Flutter Web and Docker Image for Local Registry / Build Flutter Web App (push) Has been cancelled

This commit is contained in:
whysman 2024-11-15 12:18:32 -05:00
parent 3cb6efe5bd
commit d18b576d7e

View File

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