mirror of
https://github.com/StarCitizenToolBox/app.git
synced 2026-02-06 15:10:20 +00:00
feat: PartyRoomGameLogTrackerProvider
This commit is contained in:
@@ -75,7 +75,11 @@ class PartyRoom extends _$PartyRoom {
|
||||
Box? _confBox;
|
||||
StreamSubscription<partroom.RoomEvent>? _eventStreamSubscription;
|
||||
Timer? _heartbeatTimer;
|
||||
Timer? _reconnectTimer;
|
||||
bool _disposed = false;
|
||||
int _reconnectAttempts = 0;
|
||||
static const int _maxReconnectAttempts = 5;
|
||||
static const Duration _reconnectDelay = Duration(seconds: 3);
|
||||
|
||||
@override
|
||||
PartyRoomFullState build() {
|
||||
@@ -398,6 +402,7 @@ class PartyRoom extends _$PartyRoom {
|
||||
await _stopHeartbeat();
|
||||
await _stopEventStream();
|
||||
|
||||
_reconnectAttempts = 0;
|
||||
_dismissRoom();
|
||||
|
||||
dPrint('[PartyRoom] Left room: $roomUuid');
|
||||
@@ -421,6 +426,7 @@ class PartyRoom extends _$PartyRoom {
|
||||
await _stopHeartbeat();
|
||||
await _stopEventStream();
|
||||
|
||||
_reconnectAttempts = 0;
|
||||
_dismissRoom();
|
||||
|
||||
dPrint('[PartyRoom] Dismissed room: $roomUuid');
|
||||
@@ -754,24 +760,106 @@ class PartyRoom extends _$PartyRoom {
|
||||
|
||||
_eventStreamSubscription = stream.listen(
|
||||
(event) {
|
||||
// 重置重连计数器,因为连接正常
|
||||
_reconnectAttempts = 0;
|
||||
_handleRoomEvent(event);
|
||||
},
|
||||
onError: (error) {
|
||||
dPrint('[PartyRoom] Event stream error: $error');
|
||||
// 发生错误时尝试重连
|
||||
_scheduleReconnect(roomUuid);
|
||||
},
|
||||
onDone: () {
|
||||
dPrint('[PartyRoom] Event stream closed');
|
||||
// 流关闭时尝试重连
|
||||
_scheduleReconnect(roomUuid);
|
||||
},
|
||||
);
|
||||
|
||||
dPrint('[PartyRoom] Event stream started');
|
||||
// 成功启动,重置重连计数
|
||||
_reconnectAttempts = 0;
|
||||
} catch (e) {
|
||||
dPrint('[PartyRoom] StartEventStream error: $e');
|
||||
// 启动失败时尝试重连
|
||||
_scheduleReconnect(roomUuid);
|
||||
}
|
||||
}
|
||||
|
||||
/// 调度重连
|
||||
void _scheduleReconnect(String roomUuid) {
|
||||
// 如果已经销毁或不在房间内,不重连
|
||||
if (_disposed || state.room.roomUuid == null || state.room.roomUuid != roomUuid) {
|
||||
dPrint('[PartyRoom] Skip reconnect: disposed=$_disposed, roomUuid=${state.room.roomUuid}');
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果已经有重连任务在进行,不重复调度
|
||||
if (_reconnectTimer?.isActive ?? false) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查重连次数
|
||||
if (_reconnectAttempts >= _maxReconnectAttempts) {
|
||||
dPrint('[PartyRoom] Max reconnect attempts reached ($_maxReconnectAttempts)');
|
||||
return;
|
||||
}
|
||||
|
||||
_reconnectAttempts++;
|
||||
dPrint(
|
||||
'[PartyRoom] Scheduling reconnect attempt $_reconnectAttempts/$_maxReconnectAttempts in ${_reconnectDelay.inSeconds}s',
|
||||
);
|
||||
|
||||
_reconnectTimer = Timer(_reconnectDelay, () async {
|
||||
await _attemptReconnect(roomUuid);
|
||||
});
|
||||
}
|
||||
|
||||
/// 尝试重连
|
||||
Future<void> _attemptReconnect(String roomUuid) async {
|
||||
if (_disposed || state.room.roomUuid == null || state.room.roomUuid != roomUuid) {
|
||||
dPrint('[PartyRoom] Abort reconnect: no longer in room');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
dPrint('[PartyRoom] Attempting to reconnect event stream...');
|
||||
|
||||
// 先检查自己是否还在房间内
|
||||
final client = state.client.roomClient;
|
||||
if (client == null) {
|
||||
dPrint('[PartyRoom] Reconnect failed: client not available');
|
||||
_scheduleReconnect(roomUuid);
|
||||
return;
|
||||
}
|
||||
|
||||
// 调用 getMyRoom 检查是否还在房间内
|
||||
final response = await client.getMyRoom(partroom.GetMyRoomRequest(), options: _getAuthCallOptions());
|
||||
|
||||
if (!response.hasRoom() || response.room.roomUuid != roomUuid) {
|
||||
dPrint('[PartyRoom] Reconnect failed: no longer in room');
|
||||
// 不在房间内,清理状态
|
||||
await _stopHeartbeat();
|
||||
await _stopEventStream();
|
||||
_reconnectAttempts = 0;
|
||||
_dismissRoom();
|
||||
return;
|
||||
}
|
||||
|
||||
// 确认还在房间内,重新启动事件流
|
||||
dPrint('[PartyRoom] Still in room, restarting event stream');
|
||||
await _startEventStream(roomUuid);
|
||||
} catch (e) {
|
||||
dPrint('[PartyRoom] Reconnect attempt failed: $e');
|
||||
// 重连失败,继续调度下一次重连
|
||||
_scheduleReconnect(roomUuid);
|
||||
}
|
||||
}
|
||||
|
||||
/// 停止事件流监听
|
||||
Future<void> _stopEventStream() async {
|
||||
_reconnectTimer?.cancel();
|
||||
_reconnectTimer = null;
|
||||
await _eventStreamSubscription?.cancel();
|
||||
_eventStreamSubscription = null;
|
||||
dPrint('[PartyRoom] Event stream stopped');
|
||||
@@ -794,6 +882,21 @@ class PartyRoom extends _$PartyRoom {
|
||||
case partroom.RoomEventType.MEMBER_JOINED:
|
||||
case partroom.RoomEventType.MEMBER_LEFT:
|
||||
case partroom.RoomEventType.MEMBER_KICKED:
|
||||
if (event.type == partroom.RoomEventType.MEMBER_KICKED) {
|
||||
// 判断被踢的是不是自己
|
||||
if (event.member.gameUserId == state.auth.userInfo?.gameUserId) {
|
||||
// 离开房间
|
||||
_dismissRoom();
|
||||
return;
|
||||
}
|
||||
}
|
||||
//
|
||||
if (event.type == partroom.RoomEventType.MEMBER_LEFT) {
|
||||
if (event.member.gameUserId == state.auth.userInfo?.gameUserId) {
|
||||
// 判断离开的是不是自己
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 刷新成员列表
|
||||
if (state.room.roomUuid != null) {
|
||||
getRoomMembers(state.room.roomUuid!);
|
||||
|
||||
@@ -44,7 +44,7 @@ final class PartyRoomProvider
|
||||
}
|
||||
}
|
||||
|
||||
String _$partyRoomHash() => r'02cdd156995799411eb47107d5c197f43e78629e';
|
||||
String _$partyRoomHash() => r'd7182854c8caf5bb362c45a4e6e2ab40c2ef1b09';
|
||||
|
||||
/// PartyRoom Provider
|
||||
|
||||
|
||||
Reference in New Issue
Block a user