Added functionality to reconnect if the application lifecycle changes (i.e visibility)
All checks were successful
Build Flutter Web and Docker Image for Local Registry / Build Flutter Web App (push) Successful in 7m58s
All checks were successful
Build Flutter Web and Docker Image for Local Registry / Build Flutter Web App (push) Successful in 7m58s
This commit is contained in:
parent
300a597155
commit
c5eef02ce1
@ -17,12 +17,8 @@ class StatusPage extends StatefulWidget {
|
|||||||
StatusPageState createState() => StatusPageState();
|
StatusPageState createState() => StatusPageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class StatusPageState extends State<StatusPage> {
|
class StatusPageState extends State<StatusPage> with WidgetsBindingObserver {
|
||||||
final WebSocketChannel channel = WebSocketChannel.connect(
|
WebSocketChannel? channel;
|
||||||
Uri.parse('wss://api.pogdark.com:8889/ws'),
|
|
||||||
//Uri.parse('ws://localhost:8080/ws'),
|
|
||||||
);
|
|
||||||
|
|
||||||
late final Stream<dynamic> broadcastStream;
|
late final Stream<dynamic> broadcastStream;
|
||||||
late StreamController<dynamic> controller;
|
late StreamController<dynamic> controller;
|
||||||
List<Map<String, dynamic>> messages = [];
|
List<Map<String, dynamic>> messages = [];
|
||||||
@ -31,9 +27,42 @@ class StatusPageState extends State<StatusPage> {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
WidgetsBinding.instance.addObserver(this);
|
||||||
|
_initializeWebSocket();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _initializeWebSocket() {
|
||||||
|
channel = WebSocketChannel.connect(
|
||||||
|
Uri.parse('wss://api.pogdark.com:8889/ws'),
|
||||||
|
//Uri.parse('ws://localhost:8080/ws'),
|
||||||
|
);
|
||||||
|
|
||||||
controller = StreamController<dynamic>.broadcast();
|
controller = StreamController<dynamic>.broadcast();
|
||||||
controller.addStream(channel.stream);
|
controller.addStream(channel!.stream);
|
||||||
broadcastStream = channel.stream.asBroadcastStream();
|
broadcastStream = channel!.stream.asBroadcastStream();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
WidgetsBinding.instance.removeObserver(this);
|
||||||
|
channel?.sink.close();
|
||||||
|
controller.close();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||||
|
if (state == AppLifecycleState.resumed) {
|
||||||
|
// Reestablish the WebSocket connection when the app returns to the foreground
|
||||||
|
_reconnectWebSocket();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _reconnectWebSocket() {
|
||||||
|
if (channel != null) {
|
||||||
|
channel!.sink.close();
|
||||||
|
}
|
||||||
|
_initializeWebSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Map<String, dynamic>> _getMessagesByStatus(String status) {
|
List<Map<String, dynamic>> _getMessagesByStatus(String status) {
|
||||||
@ -58,7 +87,7 @@ class StatusPageState extends State<StatusPage> {
|
|||||||
'Status': newStatus.isEmpty ? 'expired' : newStatus,
|
'Status': newStatus.isEmpty ? 'expired' : newStatus,
|
||||||
'Timestamp': DateTime.now().toIso8601String(),
|
'Timestamp': DateTime.now().toIso8601String(),
|
||||||
});
|
});
|
||||||
channel.sink.add(message);
|
channel!.sink.add(message);
|
||||||
|
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
Fluttertoast.showToast(
|
Fluttertoast.showToast(
|
||||||
@ -277,11 +306,4 @@ class StatusPageState extends State<StatusPage> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
void dispose() {
|
|
||||||
channel.sink.close();
|
|
||||||
controller.close();
|
|
||||||
super.dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user