mirror of
https://github.com/lifegpc/eh_downloader_flutter.git
synced 2026-07-08 01:30:28 +08:00
Add client ping functions to task websocket
This commit is contained in:
@@ -299,6 +299,7 @@ class _MainApp extends State<MainApp> with WidgetsBindingObserver {
|
|||||||
_lifecycleState = WidgetsBinding.instance.lifecycleState;
|
_lifecycleState = WidgetsBinding.instance.lifecycleState;
|
||||||
listener.tryEmit("lifecycle", _lifecycleState);
|
listener.tryEmit("lifecycle", _lifecycleState);
|
||||||
}
|
}
|
||||||
|
tasks.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||||
import 'api/eh.dart';
|
import 'api/eh.dart';
|
||||||
@@ -20,6 +21,7 @@ class TaskManager {
|
|||||||
bool _isFetching = false;
|
bool _isFetching = false;
|
||||||
List<int> peddingGids = [];
|
List<int> peddingGids = [];
|
||||||
List<String> peddingTokens = [];
|
List<String> peddingTokens = [];
|
||||||
|
late Timer _pingTimer;
|
||||||
void clear() {
|
void clear() {
|
||||||
tasks.clear();
|
tasks.clear();
|
||||||
_channel?.stream.drain();
|
_channel?.stream.drain();
|
||||||
@@ -187,6 +189,9 @@ class TaskManager {
|
|||||||
}
|
}
|
||||||
}, onError: (e) {
|
}, onError: (e) {
|
||||||
_log.warning("Task websocket error: $e");
|
_log.warning("Task websocket error: $e");
|
||||||
|
}, onDone: () {
|
||||||
|
_log.warning(
|
||||||
|
"WenSocket closed: ${_channel?.closeCode} ${_channel?.closeReason}");
|
||||||
if (_allowReconnect) {
|
if (_allowReconnect) {
|
||||||
_log.info("Reconnecting to task server in 5 seconds");
|
_log.info("Reconnecting to task server in 5 seconds");
|
||||||
_reconnectTimer = Timer(const Duration(seconds: 5), () {
|
_reconnectTimer = Timer(const Duration(seconds: 5), () {
|
||||||
@@ -214,4 +219,28 @@ class TaskManager {
|
|||||||
void sendTaskList() {
|
void sendTaskList() {
|
||||||
_channel?.sink.add("{\"type\":\"task_list\"}");
|
_channel?.sink.add("{\"type\":\"task_list\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _onLifeCycleChanged(dynamic arg) {
|
||||||
|
final state = arg as AppLifecycleState?;
|
||||||
|
if (state == null) return;
|
||||||
|
if (state == AppLifecycleState.resumed) {
|
||||||
|
try {
|
||||||
|
_channel?.sink.add("{\"type\":\"ping\"}");
|
||||||
|
} catch (e) {
|
||||||
|
_log.warning("Failed to send ping when onResumed: $e");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void init() {
|
||||||
|
listener.on("lifecycle", _onLifeCycleChanged);
|
||||||
|
_pingTimer = Timer.periodic(const Duration(seconds: 30), (timer) {
|
||||||
|
try {
|
||||||
|
_channel?.sink.add("{\"type\":\"ping\"}");
|
||||||
|
} catch (e) {
|
||||||
|
_log.warning("Failed to send ping: $e");
|
||||||
|
}
|
||||||
|
_pingTimer = timer;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user