完成修复tcp在app的各种操作,包括 切换,推出重新加入,息屏后重新进入重连失败问题.-已测试成功验证
This commit is contained in:
@@ -49,9 +49,9 @@ class AuthCubit extends Cubit<AuthState> {
|
||||
data: {'user': user, 'data': user}
|
||||
);
|
||||
if (user != null) {
|
||||
tcp.connect(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT);
|
||||
await tcp.connect(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT);
|
||||
// await _authTcpDatasource.sendAuthPacket();//包括发送认证包和获取列表和切换函数
|
||||
tcp.startHeartbeat(interval: const Duration(seconds: 4));
|
||||
tcp.startHeartbeat(interval: const Duration(seconds: 4));
|
||||
// 2. 同步全局 App 状态
|
||||
appCubit.setAuth(user);
|
||||
// 3. 进入已登录状态
|
||||
@@ -142,4 +142,27 @@ class AuthCubit extends Cubit<AuthState> {
|
||||
_kickOutSub?.cancel(); // 销毁 Cubit 时关闭监听
|
||||
return super.close();
|
||||
}
|
||||
|
||||
|
||||
/// 🔥 息屏/后台后恢复到前台时的重连方法
|
||||
Future<void> reconnectAfterResume() async {
|
||||
print('📱 [AUTH] 检测到应用恢复到前台,检查 TCP 连接状态...');
|
||||
|
||||
// 如果 TCP 未连接,则执行重连
|
||||
if (!tcp.isConnected) {
|
||||
print('⚠️ [AUTH] TCP 未连接,开始重连...');
|
||||
try {
|
||||
await tcp.connect(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT);
|
||||
tcp.startHeartbeat(interval: const Duration(seconds: 4));
|
||||
print('✅ [AUTH] TCP 重连成功!');
|
||||
} catch (e) {
|
||||
print('❌ [AUTH] TCP 重连失败:$e');
|
||||
// 可以选择重试或者通知用户
|
||||
}
|
||||
} else {
|
||||
print('✅ [AUTH] TCP 连接正常,无需重连');
|
||||
// 可选:发送一个心跳包确认连接有效
|
||||
tcp.sendHeartbeat();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user