Browse Source

socketclient remove reconnect time

wr_202303
zsw 4 months ago
parent
commit
0fb67d8cf0
  1. 24
      lib/im/SocketClient.dart

24
lib/im/SocketClient.dart

@ -66,10 +66,12 @@ class SocketClient {
}).catchError((error) { }).catchError((error) {
debugPrint("socket-connect-error: $error"); debugPrint("socket-connect-error: $error");
showDebugToast("socket-connect-error: $error"); showDebugToast("socket-connect-error: $error");
reconnectTime = 1500;
_socket = null; _socket = null;
reconnect(); reconnect();
}); });
// checkConnect();
} }
showDebugToast(text) { showDebugToast(text) {
@ -89,17 +91,19 @@ class SocketClient {
timer.cancel(); timer.cancel();
return; return;
} }
try {
_socket.add(proto2.toBytes()); _socket.add(proto2.toBytes());
} catch (e) {
debugPrint("socket-send-error: ${e.toString()}");
showDebugToast("socket-send-error: ${e.toString()}");
reconnect();
}
}); });
} }
int reconnectTime = 1500;
reconnect() { reconnect() {
Future.delayed(Duration(milliseconds: reconnectTime), () {
dispose(); dispose();
connect(); connect();
});
} }
Map<String, Function(Message message)> callbacks = <String, Function(Message message)>{}; Map<String, Function(Message message)> callbacks = <String, Function(Message message)>{};
@ -149,14 +153,22 @@ class SocketClient {
Uint8List data = utf8.encode(content); Uint8List data = utf8.encode(content);
MsgData msgData = MsgData(to: toId, from: userId, type: MsgType.TEXT, data: data); MsgData msgData = MsgData(to: toId, from: userId, type: MsgType.TEXT, data: data);
final proto2 = Proto(5, 1, msgData.writeToBuffer()); final proto2 = Proto(5, 1, msgData.writeToBuffer());
try {
_socket.add(proto2.toBytes()); _socket.add(proto2.toBytes());
} catch (e) {
hxDatabase.update({"id": id, "state": 3}).catchError((error) {
debugPrint("insertMessage: ${error.toString()}");
});
debugPrint("socket-send-error: ${e.toString()}");
showDebugToast("socket-send-error: ${e.toString()}");
reconnect();
}
debugPrint("sendMessage: ${message["id"]}"); debugPrint("sendMessage: ${message["id"]}");
return Message.fromJson(message); return Message.fromJson(message);
} }
checkSocket() { checkSocket() {
if (_socket == null) { if (_socket == null) {
reconnectTime = 1000;
reconnect(); reconnect();
return false; return false;
} }

Loading…
Cancel
Save