登录被顶 超时

This commit is contained in:
WoNiu 2024-04-06 17:00:54 +08:00
parent 51d33a1137
commit 229965a3c9
1 changed files with 13 additions and 9 deletions

View File

@ -43,8 +43,9 @@ class LoginSocket{
connect() async { connect() async {
socket?.close();
socket = await Socket.connect(url, port, timeout: const Duration(seconds: 30)); socket = await Socket.connect(url, port, timeout: const Duration(seconds: 30));
heartTime = DateTime.now().millisecondsSinceEpoch;
socket?.listen((event) { socket?.listen((event) {
@ -58,6 +59,11 @@ class LoginSocket{
// //
if (data['op'] == 4){ if (data['op'] == 4){
try{ try{
//
heartTime = DateTime.now().millisecondsSinceEpoch;
//
int time = data['time']; int time = data['time'];
time = DateTime.now().millisecondsSinceEpoch - time; time = DateTime.now().millisecondsSinceEpoch - time;
if(time> (3 * 60 * 1000) ){ // > 3 if(time> (3 * 60 * 1000) ){ // > 3
@ -107,17 +113,17 @@ class LoginSocket{
/// ///
heartbeat(){ heartbeat(){
DateTime now = DateTime.now();
int timestamp = now.millisecondsSinceEpoch; //
heartTime = timestamp;
Timer(const Duration(seconds: 20), () async { Timer(const Duration(seconds: 20), () async {
if (socket == null){ final time = DateTime.now().millisecondsSinceEpoch - heartTime;
// if(time > (3 * 60 * 1000) ){ // > 3
await connect(); socketError();
return;
} }
heartbeat();
Map map = { Map map = {
"op": 3, "op": 3,
}; };
@ -125,8 +131,6 @@ class LoginSocket{
socket?.add(dataMake(map)); socket?.add(dataMake(map));
socket?.flush(); socket?.flush();
heartbeat();
}); });
} }