Compare commits
2 Commits
b6b6e41e8b
...
525e10badc
| Author | SHA1 | Date |
|---|---|---|
|
|
525e10badc | |
|
|
229965a3c9 |
|
|
@ -45,6 +45,7 @@ class LoginSocket{
|
||||||
connect() async {
|
connect() async {
|
||||||
socket?.close();
|
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();
|
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue