|
|
|
@ -20,6 +20,8 @@ class SocketClient {
|
|
|
|
|
final num port = 9090; |
|
|
|
|
Socket _socket; |
|
|
|
|
SharedPreferences _shared; |
|
|
|
|
Timer timer; |
|
|
|
|
bool get heartbeatActive => timer != null && timer.isActive; |
|
|
|
|
|
|
|
|
|
connect() async { |
|
|
|
|
_shared = await SharedPreferences.getInstance(); |
|
|
|
@ -41,17 +43,17 @@ class SocketClient {
|
|
|
|
|
print('收到来自:${dataResult.from},消息内容: ${utf8.decode(dataResult.data)} '); |
|
|
|
|
|
|
|
|
|
Map<String, dynamic> messageMap = createMessage(userId, utf8.decode(dataResult.data), msgType: dataResult.type.value, userId: dataResult.from); |
|
|
|
|
Message message = Message.fromJson(messageMap); |
|
|
|
|
callbacks[userId]?.call(message); /// user self conversation list callback |
|
|
|
|
|
|
|
|
|
if (callbacks[dataResult.from] != null) { |
|
|
|
|
messageMap["state"] = 1; |
|
|
|
|
message.state = 1; |
|
|
|
|
} |
|
|
|
|
hxDatabase.insert(messageMap).then((value) { |
|
|
|
|
messageMap["id"] = value; |
|
|
|
|
Message message = Message.fromJson(messageMap); |
|
|
|
|
if (callbacks[dataResult.from] != null) { |
|
|
|
|
callbacks[dataResult.from].call(message); /// user conversation callback |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
hxDatabase.insert(messageMap); |
|
|
|
|
|
|
|
|
|
callbacks[userId]?.call(message); /// user self conversation list callback |
|
|
|
|
}); |
|
|
|
|
}, onError: (Object error, StackTrace stackTrace) { |
|
|
|
|
debugPrint("socket-listen-error: $error, stackTrace: $stackTrace"); |
|
|
|
|
showDebugToast("socket-listen-error: $error, stackTrace: $stackTrace"); |
|
|
|
@ -69,9 +71,6 @@ class SocketClient {
|
|
|
|
|
_socket = null; |
|
|
|
|
reconnect(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// checkConnect(); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
showDebugToast(text) { |
|
|
|
@ -80,25 +79,51 @@ class SocketClient {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
heartbeat() { |
|
|
|
|
Timer.periodic(const Duration(milliseconds: 30000), (timer) { |
|
|
|
|
int millisecondsTime = DateTime.now().millisecondsSinceEpoch; |
|
|
|
|
debugPrint("heartbeat: $millisecondsTime"); |
|
|
|
|
Proto heartbeatData() { |
|
|
|
|
DateTime dateTime = DateTime.now(); |
|
|
|
|
int millisecondsTime = dateTime.millisecondsSinceEpoch; |
|
|
|
|
Uint8List data = utf8.encode(jsonEncode({"heartbeat": millisecondsTime})); |
|
|
|
|
MsgData msgData = MsgData(from: userId, type: MsgType.TEXT, data: data); |
|
|
|
|
final proto2 = Proto(3, 1, msgData.writeToBuffer()); |
|
|
|
|
debugPrint("heartbeat: ${dateTime.toString()}"); |
|
|
|
|
return proto2; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
heartbeat() { |
|
|
|
|
cancelTimer(); |
|
|
|
|
timer = Timer.periodic(const Duration(milliseconds: 30000), (timer) { |
|
|
|
|
if(!checkSocket()) { |
|
|
|
|
timer.cancel(); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
sendHeartBeatAndCheckSocket(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// send heartBeat and check socket is connected |
|
|
|
|
/// send error: reconnect, |
|
|
|
|
/// else check Timer.periodic isActive , |
|
|
|
|
/// if not active: Timer.periodic send heartBeat |
|
|
|
|
sendHeartBeatAndCheckSocket() { |
|
|
|
|
final proto2 = heartbeatData(); |
|
|
|
|
try { |
|
|
|
|
_socket.add(proto2.toBytes()); |
|
|
|
|
if (!socketClient.heartbeatActive) { |
|
|
|
|
heartbeat(); |
|
|
|
|
debugPrint("socket-periodic-send-heart-beat"); |
|
|
|
|
} |
|
|
|
|
} catch (e) { |
|
|
|
|
debugPrint("socket-send-error: ${e.toString()}"); |
|
|
|
|
showDebugToast("socket-send-error: ${e.toString()}"); |
|
|
|
|
reconnect(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cancelTimer() { |
|
|
|
|
if (timer != null && timer.isActive) { |
|
|
|
|
timer.cancel(); |
|
|
|
|
timer = null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
reconnect() { |
|
|
|
|