diff --git a/lib/im/SocketClient.dart b/lib/im/SocketClient.dart index 950eacdc..4b45ec73 100644 --- a/lib/im/SocketClient.dart +++ b/lib/im/SocketClient.dart @@ -66,10 +66,12 @@ class SocketClient { }).catchError((error) { debugPrint("socket-connect-error: $error"); showDebugToast("socket-connect-error: $error"); - reconnectTime = 1500; _socket = null; reconnect(); }); + + // checkConnect(); + } showDebugToast(text) { @@ -89,17 +91,19 @@ class SocketClient { timer.cancel(); return; } - _socket.add(proto2.toBytes()); + try { + _socket.add(proto2.toBytes()); + } catch (e) { + debugPrint("socket-send-error: ${e.toString()}"); + showDebugToast("socket-send-error: ${e.toString()}"); + reconnect(); + } }); } - int reconnectTime = 1500; - reconnect() { - Future.delayed(Duration(milliseconds: reconnectTime), () { - dispose(); - connect(); - }); + dispose(); + connect(); } Map callbacks = {}; @@ -149,14 +153,22 @@ class SocketClient { Uint8List data = utf8.encode(content); MsgData msgData = MsgData(to: toId, from: userId, type: MsgType.TEXT, data: data); final proto2 = Proto(5, 1, msgData.writeToBuffer()); - _socket.add(proto2.toBytes()); + try { + _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"]}"); return Message.fromJson(message); } checkSocket() { if (_socket == null) { - reconnectTime = 1000; reconnect(); return false; }