替换dowhile实现方式

This commit is contained in:
2026-03-12 09:11:20 +08:00
parent a8e4ad5a20
commit a1d0842a44
2 changed files with 30 additions and 7 deletions

View File

@@ -23,9 +23,13 @@ class MongoDb implements DataBase {
await db.open(); await db.open();
print('MongoDb Connected successfully!'); print('MongoDb Connected successfully!');
//定时检测数据库是否断开 //定时检测数据库是否断开
do { // do {
await Future.delayed(Duration(seconds: 5)); // await Future.delayed(Duration(seconds: 5));
} while (db.isConnected); // } while (db.isConnected);
await Future.doWhile(() async {
await Future.delayed(Duration(seconds: 1));
return db.isConnected;
});
connect(true); connect(true);
} catch (e) { } catch (e) {
print('MongoDb Connection error: $e'); print('MongoDb Connection error: $e');

View File

@@ -51,12 +51,12 @@ class Redis {
_connected = true; _connected = true;
//定时检测是否断开连接 //定时检测是否断开连接
Future.delayed(Duration(seconds: 1), () async { Future.delayed(Duration(seconds: 1), () async {
do { await Future.doWhile(() async {
await Future.delayed(Duration(seconds: 5)); await Future.delayed(Duration(seconds: 5));
try { try {
var r = await _command!.send_object(["PING"]); var r = await _command!.send_object(["PING"]);
if (r != "PONG") { if (r != "PONG") {
break; return false;
} }
} catch (e) { } catch (e) {
//发送数据失败 //发送数据失败
@@ -66,9 +66,28 @@ class Redis {
} catch (o) { } catch (o) {
// //
} }
break; return false;
} }
} while (true); return true;
});
// do {
// await Future.delayed(Duration(seconds: 5));
// try {
// var r = await _command!.send_object(["PING"]);
// if (r != "PONG") {
// break;
// }
// } catch (e) {
// //发送数据失败
// try {
// print('Redis Connection check failed: $e');
// _command!.get_connection().close();
// } catch (o) {
// //
// }
// break;
// }
// } while (true);
connect(reconnect: true); connect(reconnect: true);
}); });
} catch (e) { } catch (e) {