Browse Source

Merge remote-tracking branch 'origin/wr_202303' into wr_202303

wr_202303
wurong 4 months ago
parent
commit
5d13000356
  1. 37
      lib/im/SocketClient.dart

37
lib/im/SocketClient.dart

@ -4,6 +4,8 @@ import 'dart:core';
import 'dart:core';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:huixiang/im/Proto.dart';
import 'package:huixiang/im/database/message.dart';
import 'package:huixiang/im/out/auth.pb.dart';
@ -17,11 +19,17 @@ class SocketClient {
final String ip = kDebugMode ? '192.168.10.129' : '47.93.216.24';
final num port = 9090;
Socket _socket;
SharedPreferences shared;
SharedPreferences _shared;
connect() async {
shared = await SharedPreferences.getInstance();
_shared = await SharedPreferences.getInstance();
if (_socket != null) {
reconnect();
return;
}
showDebugToast("socket-connect .... ");
await Socket.connect(ip, port).then((value) {
debugPrint("socket-connect-$ip");
_socket = value;
@ -34,10 +42,9 @@ class SocketClient {
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 callback
callbacks[userId]?.call(message); /// user self conversation list callback
if (callbacks[dataResult.from] != null) {
//
messageMap["state"] = 1;
message.state = 1;
callbacks[dataResult.from].call(message); /// user conversation callback
@ -47,27 +54,35 @@ class SocketClient {
}, onError: (Object error, StackTrace stackTrace) {
debugPrint("socket-listen-error: $error, stackTrace: $stackTrace");
showDebugToast("socket-listen-error: $error, stackTrace: $stackTrace");
reconnect();
}, onDone: () {
debugPrint("socket-listen-down: down");
});
authRequest(shared.getString("token"));
authRequest(_shared.getString("token"));
heartbeat();
}).catchError((error) {
debugPrint("socket-connect-error: $error");
showDebugToast("socket-connect-error: $error");
reconnectTime = 1500;
_socket = null;
reconnect();
});
}
showDebugToast(text) {
if (kDebugMode) {
SmartDialog.showToast(text, alignment: Alignment.center);
}
}
heartbeat() {
Timer.periodic(const Duration(milliseconds: 30000), (timer) {
int milliseTime = DateTime.now().millisecondsSinceEpoch;
debugPrint("heartbeat: ${milliseTime}");
Uint8List data = utf8.encode(jsonEncode({"heartbeat": milliseTime}));
int millisecondsTime = DateTime.now().millisecondsSinceEpoch;
debugPrint("heartbeat: $millisecondsTime");
Uint8List data = utf8.encode(jsonEncode({"heartbeat": millisecondsTime}));
MsgData msgData = MsgData(from: userId, type: MsgType.TEXT, data: data);
final proto2 = Proto(3, 1, msgData.writeToBuffer());
if(!checkSocket()) {
@ -78,10 +93,10 @@ class SocketClient {
});
}
int reconnectTime = 600;
int reconnectTime = 1500;
reconnect() {
Future.delayed(Duration(milliseconds: reconnectTime *= 2), () {
Future.delayed(Duration(milliseconds: reconnectTime), () {
dispose();
connect();
});
@ -148,6 +163,6 @@ class SocketClient {
return true;
}
String get userId => shared.getString("userId");
String get userId => _shared.getString("userId");
}
Loading…
Cancel
Save