feat: update messages

This commit is contained in:
xkeyC
2025-11-24 12:25:32 +08:00
parent 078eae8e52
commit 4718b4627b
8 changed files with 309 additions and 202 deletions

View File

@@ -39,6 +39,7 @@ sealed class PartyRoomState with _$PartyRoomState {
@Default(false) bool isOwner,
String? roomUuid,
@Default([]) List<partroom.RoomEvent> recentEvents,
@Default(false) bool eventStreamDisconnected,
}) = _PartyRoomState;
}
@@ -75,11 +76,7 @@ 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() {
@@ -402,7 +399,6 @@ class PartyRoom extends _$PartyRoom {
await _stopHeartbeat();
await _stopEventStream();
_reconnectAttempts = 0;
_dismissRoom();
dPrint('[PartyRoom] Left room: $roomUuid');
@@ -426,7 +422,6 @@ class PartyRoom extends _$PartyRoom {
await _stopHeartbeat();
await _stopEventStream();
_reconnectAttempts = 0;
_dismissRoom();
dPrint('[PartyRoom] Dismissed room: $roomUuid');
@@ -760,106 +755,35 @@ class PartyRoom extends _$PartyRoom {
_eventStreamSubscription = stream.listen(
(event) {
// 重置重连计数器,因为连接正常
_reconnectAttempts = 0;
_handleRoomEvent(event);
},
onError: (error) {
dPrint('[PartyRoom] Event stream error: $error');
// 发生错误时尝试重连
_scheduleReconnect(roomUuid);
// 标记事件流断开
state = state.copyWith(room: state.room.copyWith(eventStreamDisconnected: true));
},
onDone: () {
dPrint('[PartyRoom] Event stream closed');
// 流关闭时尝试重连
_scheduleReconnect(roomUuid);
// 标记事件流断开
state = state.copyWith(room: state.room.copyWith(eventStreamDisconnected: true));
},
);
// 成功启动,重置断开标记
state = state.copyWith(room: state.room.copyWith(eventStreamDisconnected: false));
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);
// 启动失败,标记断开
state = state.copyWith(room: state.room.copyWith(eventStreamDisconnected: true));
rethrow;
}
}
/// 停止事件流监听
Future<void> _stopEventStream() async {
_reconnectTimer?.cancel();
_reconnectTimer = null;
if (_eventStreamSubscription == null) return;
await _eventStreamSubscription?.cancel();
_eventStreamSubscription = null;
dPrint('[PartyRoom] Event stream stopped');
@@ -948,6 +872,43 @@ class PartyRoom extends _$PartyRoom {
}
}
// ========== 手动重连方法 ==========
/// 刷新房间信息并重新启动事件流
/// 供 UI 层在检测到连接断开时调用
Future<void> refreshRoomAndReconnect() async {
try {
final roomUuid = state.room.roomUuid;
if (roomUuid == null) {
throw Exception('Not in a room');
}
dPrint('[PartyRoom] Refreshing room and reconnecting...');
// 刷新房间信息
await getRoomInfo(roomUuid);
// 刷新成员列表
await getRoomMembers(roomUuid);
// 重新启动事件流
await _startEventStream(roomUuid);
dPrint('[PartyRoom] Room refreshed and reconnected successfully');
} catch (e) {
dPrint('[PartyRoom] Refresh and reconnect error: $e');
rethrow;
}
}
/// 确认已看到断开连接通知(用于清除断开标记而不重连)
void acknowledgeDisconnection() {
state = state.copyWith(room: state.room.copyWith(eventStreamDisconnected: false));
}
/// 检查事件流是否处于活跃状态
bool get isEventStreamActive => _eventStreamSubscription != null && !(_eventStreamSubscription?.isPaused ?? true);
// ========== 通用服务方法 ==========
/// 获取服务器时间

View File

@@ -277,7 +277,7 @@ as DateTime?,
/// @nodoc
mixin _$PartyRoomState {
partroom.RoomInfo? get currentRoom; List<partroom.RoomMember> get members; Map<String, common.Tag> get tags; Map<String, common.SignalType> get signalTypes; bool get isInRoom; bool get isOwner; String? get roomUuid; List<partroom.RoomEvent> get recentEvents;
partroom.RoomInfo? get currentRoom; List<partroom.RoomMember> get members; Map<String, common.Tag> get tags; Map<String, common.SignalType> get signalTypes; bool get isInRoom; bool get isOwner; String? get roomUuid; List<partroom.RoomEvent> get recentEvents; bool get eventStreamDisconnected;
/// Create a copy of PartyRoomState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@@ -288,16 +288,16 @@ $PartyRoomStateCopyWith<PartyRoomState> get copyWith => _$PartyRoomStateCopyWith
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is PartyRoomState&&(identical(other.currentRoom, currentRoom) || other.currentRoom == currentRoom)&&const DeepCollectionEquality().equals(other.members, members)&&const DeepCollectionEquality().equals(other.tags, tags)&&const DeepCollectionEquality().equals(other.signalTypes, signalTypes)&&(identical(other.isInRoom, isInRoom) || other.isInRoom == isInRoom)&&(identical(other.isOwner, isOwner) || other.isOwner == isOwner)&&(identical(other.roomUuid, roomUuid) || other.roomUuid == roomUuid)&&const DeepCollectionEquality().equals(other.recentEvents, recentEvents));
return identical(this, other) || (other.runtimeType == runtimeType&&other is PartyRoomState&&(identical(other.currentRoom, currentRoom) || other.currentRoom == currentRoom)&&const DeepCollectionEquality().equals(other.members, members)&&const DeepCollectionEquality().equals(other.tags, tags)&&const DeepCollectionEquality().equals(other.signalTypes, signalTypes)&&(identical(other.isInRoom, isInRoom) || other.isInRoom == isInRoom)&&(identical(other.isOwner, isOwner) || other.isOwner == isOwner)&&(identical(other.roomUuid, roomUuid) || other.roomUuid == roomUuid)&&const DeepCollectionEquality().equals(other.recentEvents, recentEvents)&&(identical(other.eventStreamDisconnected, eventStreamDisconnected) || other.eventStreamDisconnected == eventStreamDisconnected));
}
@override
int get hashCode => Object.hash(runtimeType,currentRoom,const DeepCollectionEquality().hash(members),const DeepCollectionEquality().hash(tags),const DeepCollectionEquality().hash(signalTypes),isInRoom,isOwner,roomUuid,const DeepCollectionEquality().hash(recentEvents));
int get hashCode => Object.hash(runtimeType,currentRoom,const DeepCollectionEquality().hash(members),const DeepCollectionEquality().hash(tags),const DeepCollectionEquality().hash(signalTypes),isInRoom,isOwner,roomUuid,const DeepCollectionEquality().hash(recentEvents),eventStreamDisconnected);
@override
String toString() {
return 'PartyRoomState(currentRoom: $currentRoom, members: $members, tags: $tags, signalTypes: $signalTypes, isInRoom: $isInRoom, isOwner: $isOwner, roomUuid: $roomUuid, recentEvents: $recentEvents)';
return 'PartyRoomState(currentRoom: $currentRoom, members: $members, tags: $tags, signalTypes: $signalTypes, isInRoom: $isInRoom, isOwner: $isOwner, roomUuid: $roomUuid, recentEvents: $recentEvents, eventStreamDisconnected: $eventStreamDisconnected)';
}
@@ -308,7 +308,7 @@ abstract mixin class $PartyRoomStateCopyWith<$Res> {
factory $PartyRoomStateCopyWith(PartyRoomState value, $Res Function(PartyRoomState) _then) = _$PartyRoomStateCopyWithImpl;
@useResult
$Res call({
partroom.RoomInfo? currentRoom, List<partroom.RoomMember> members, Map<String, common.Tag> tags, Map<String, common.SignalType> signalTypes, bool isInRoom, bool isOwner, String? roomUuid, List<partroom.RoomEvent> recentEvents
partroom.RoomInfo? currentRoom, List<partroom.RoomMember> members, Map<String, common.Tag> tags, Map<String, common.SignalType> signalTypes, bool isInRoom, bool isOwner, String? roomUuid, List<partroom.RoomEvent> recentEvents, bool eventStreamDisconnected
});
@@ -325,7 +325,7 @@ class _$PartyRoomStateCopyWithImpl<$Res>
/// Create a copy of PartyRoomState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline') @override $Res call({Object? currentRoom = freezed,Object? members = null,Object? tags = null,Object? signalTypes = null,Object? isInRoom = null,Object? isOwner = null,Object? roomUuid = freezed,Object? recentEvents = null,}) {
@pragma('vm:prefer-inline') @override $Res call({Object? currentRoom = freezed,Object? members = null,Object? tags = null,Object? signalTypes = null,Object? isInRoom = null,Object? isOwner = null,Object? roomUuid = freezed,Object? recentEvents = null,Object? eventStreamDisconnected = null,}) {
return _then(_self.copyWith(
currentRoom: freezed == currentRoom ? _self.currentRoom : currentRoom // ignore: cast_nullable_to_non_nullable
as partroom.RoomInfo?,members: null == members ? _self.members : members // ignore: cast_nullable_to_non_nullable
@@ -335,7 +335,8 @@ as Map<String, common.SignalType>,isInRoom: null == isInRoom ? _self.isInRoom :
as bool,isOwner: null == isOwner ? _self.isOwner : isOwner // ignore: cast_nullable_to_non_nullable
as bool,roomUuid: freezed == roomUuid ? _self.roomUuid : roomUuid // ignore: cast_nullable_to_non_nullable
as String?,recentEvents: null == recentEvents ? _self.recentEvents : recentEvents // ignore: cast_nullable_to_non_nullable
as List<partroom.RoomEvent>,
as List<partroom.RoomEvent>,eventStreamDisconnected: null == eventStreamDisconnected ? _self.eventStreamDisconnected : eventStreamDisconnected // ignore: cast_nullable_to_non_nullable
as bool,
));
}
@@ -417,10 +418,10 @@ return $default(_that);case _:
/// }
/// ```
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( partroom.RoomInfo? currentRoom, List<partroom.RoomMember> members, Map<String, common.Tag> tags, Map<String, common.SignalType> signalTypes, bool isInRoom, bool isOwner, String? roomUuid, List<partroom.RoomEvent> recentEvents)? $default,{required TResult orElse(),}) {final _that = this;
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( partroom.RoomInfo? currentRoom, List<partroom.RoomMember> members, Map<String, common.Tag> tags, Map<String, common.SignalType> signalTypes, bool isInRoom, bool isOwner, String? roomUuid, List<partroom.RoomEvent> recentEvents, bool eventStreamDisconnected)? $default,{required TResult orElse(),}) {final _that = this;
switch (_that) {
case _PartyRoomState() when $default != null:
return $default(_that.currentRoom,_that.members,_that.tags,_that.signalTypes,_that.isInRoom,_that.isOwner,_that.roomUuid,_that.recentEvents);case _:
return $default(_that.currentRoom,_that.members,_that.tags,_that.signalTypes,_that.isInRoom,_that.isOwner,_that.roomUuid,_that.recentEvents,_that.eventStreamDisconnected);case _:
return orElse();
}
@@ -438,10 +439,10 @@ return $default(_that.currentRoom,_that.members,_that.tags,_that.signalTypes,_th
/// }
/// ```
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( partroom.RoomInfo? currentRoom, List<partroom.RoomMember> members, Map<String, common.Tag> tags, Map<String, common.SignalType> signalTypes, bool isInRoom, bool isOwner, String? roomUuid, List<partroom.RoomEvent> recentEvents) $default,) {final _that = this;
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( partroom.RoomInfo? currentRoom, List<partroom.RoomMember> members, Map<String, common.Tag> tags, Map<String, common.SignalType> signalTypes, bool isInRoom, bool isOwner, String? roomUuid, List<partroom.RoomEvent> recentEvents, bool eventStreamDisconnected) $default,) {final _that = this;
switch (_that) {
case _PartyRoomState():
return $default(_that.currentRoom,_that.members,_that.tags,_that.signalTypes,_that.isInRoom,_that.isOwner,_that.roomUuid,_that.recentEvents);}
return $default(_that.currentRoom,_that.members,_that.tags,_that.signalTypes,_that.isInRoom,_that.isOwner,_that.roomUuid,_that.recentEvents,_that.eventStreamDisconnected);}
}
/// A variant of `when` that fallback to returning `null`
///
@@ -455,10 +456,10 @@ return $default(_that.currentRoom,_that.members,_that.tags,_that.signalTypes,_th
/// }
/// ```
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( partroom.RoomInfo? currentRoom, List<partroom.RoomMember> members, Map<String, common.Tag> tags, Map<String, common.SignalType> signalTypes, bool isInRoom, bool isOwner, String? roomUuid, List<partroom.RoomEvent> recentEvents)? $default,) {final _that = this;
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( partroom.RoomInfo? currentRoom, List<partroom.RoomMember> members, Map<String, common.Tag> tags, Map<String, common.SignalType> signalTypes, bool isInRoom, bool isOwner, String? roomUuid, List<partroom.RoomEvent> recentEvents, bool eventStreamDisconnected)? $default,) {final _that = this;
switch (_that) {
case _PartyRoomState() when $default != null:
return $default(_that.currentRoom,_that.members,_that.tags,_that.signalTypes,_that.isInRoom,_that.isOwner,_that.roomUuid,_that.recentEvents);case _:
return $default(_that.currentRoom,_that.members,_that.tags,_that.signalTypes,_that.isInRoom,_that.isOwner,_that.roomUuid,_that.recentEvents,_that.eventStreamDisconnected);case _:
return null;
}
@@ -470,7 +471,7 @@ return $default(_that.currentRoom,_that.members,_that.tags,_that.signalTypes,_th
class _PartyRoomState implements PartyRoomState {
const _PartyRoomState({this.currentRoom, final List<partroom.RoomMember> members = const [], final Map<String, common.Tag> tags = const {}, final Map<String, common.SignalType> signalTypes = const {}, this.isInRoom = false, this.isOwner = false, this.roomUuid, final List<partroom.RoomEvent> recentEvents = const []}): _members = members,_tags = tags,_signalTypes = signalTypes,_recentEvents = recentEvents;
const _PartyRoomState({this.currentRoom, final List<partroom.RoomMember> members = const [], final Map<String, common.Tag> tags = const {}, final Map<String, common.SignalType> signalTypes = const {}, this.isInRoom = false, this.isOwner = false, this.roomUuid, final List<partroom.RoomEvent> recentEvents = const [], this.eventStreamDisconnected = false}): _members = members,_tags = tags,_signalTypes = signalTypes,_recentEvents = recentEvents;
@override final partroom.RoomInfo? currentRoom;
@@ -505,6 +506,7 @@ class _PartyRoomState implements PartyRoomState {
return EqualUnmodifiableListView(_recentEvents);
}
@override@JsonKey() final bool eventStreamDisconnected;
/// Create a copy of PartyRoomState
/// with the given fields replaced by the non-null parameter values.
@@ -516,16 +518,16 @@ _$PartyRoomStateCopyWith<_PartyRoomState> get copyWith => __$PartyRoomStateCopyW
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is _PartyRoomState&&(identical(other.currentRoom, currentRoom) || other.currentRoom == currentRoom)&&const DeepCollectionEquality().equals(other._members, _members)&&const DeepCollectionEquality().equals(other._tags, _tags)&&const DeepCollectionEquality().equals(other._signalTypes, _signalTypes)&&(identical(other.isInRoom, isInRoom) || other.isInRoom == isInRoom)&&(identical(other.isOwner, isOwner) || other.isOwner == isOwner)&&(identical(other.roomUuid, roomUuid) || other.roomUuid == roomUuid)&&const DeepCollectionEquality().equals(other._recentEvents, _recentEvents));
return identical(this, other) || (other.runtimeType == runtimeType&&other is _PartyRoomState&&(identical(other.currentRoom, currentRoom) || other.currentRoom == currentRoom)&&const DeepCollectionEquality().equals(other._members, _members)&&const DeepCollectionEquality().equals(other._tags, _tags)&&const DeepCollectionEquality().equals(other._signalTypes, _signalTypes)&&(identical(other.isInRoom, isInRoom) || other.isInRoom == isInRoom)&&(identical(other.isOwner, isOwner) || other.isOwner == isOwner)&&(identical(other.roomUuid, roomUuid) || other.roomUuid == roomUuid)&&const DeepCollectionEquality().equals(other._recentEvents, _recentEvents)&&(identical(other.eventStreamDisconnected, eventStreamDisconnected) || other.eventStreamDisconnected == eventStreamDisconnected));
}
@override
int get hashCode => Object.hash(runtimeType,currentRoom,const DeepCollectionEquality().hash(_members),const DeepCollectionEquality().hash(_tags),const DeepCollectionEquality().hash(_signalTypes),isInRoom,isOwner,roomUuid,const DeepCollectionEquality().hash(_recentEvents));
int get hashCode => Object.hash(runtimeType,currentRoom,const DeepCollectionEquality().hash(_members),const DeepCollectionEquality().hash(_tags),const DeepCollectionEquality().hash(_signalTypes),isInRoom,isOwner,roomUuid,const DeepCollectionEquality().hash(_recentEvents),eventStreamDisconnected);
@override
String toString() {
return 'PartyRoomState(currentRoom: $currentRoom, members: $members, tags: $tags, signalTypes: $signalTypes, isInRoom: $isInRoom, isOwner: $isOwner, roomUuid: $roomUuid, recentEvents: $recentEvents)';
return 'PartyRoomState(currentRoom: $currentRoom, members: $members, tags: $tags, signalTypes: $signalTypes, isInRoom: $isInRoom, isOwner: $isOwner, roomUuid: $roomUuid, recentEvents: $recentEvents, eventStreamDisconnected: $eventStreamDisconnected)';
}
@@ -536,7 +538,7 @@ abstract mixin class _$PartyRoomStateCopyWith<$Res> implements $PartyRoomStateCo
factory _$PartyRoomStateCopyWith(_PartyRoomState value, $Res Function(_PartyRoomState) _then) = __$PartyRoomStateCopyWithImpl;
@override @useResult
$Res call({
partroom.RoomInfo? currentRoom, List<partroom.RoomMember> members, Map<String, common.Tag> tags, Map<String, common.SignalType> signalTypes, bool isInRoom, bool isOwner, String? roomUuid, List<partroom.RoomEvent> recentEvents
partroom.RoomInfo? currentRoom, List<partroom.RoomMember> members, Map<String, common.Tag> tags, Map<String, common.SignalType> signalTypes, bool isInRoom, bool isOwner, String? roomUuid, List<partroom.RoomEvent> recentEvents, bool eventStreamDisconnected
});
@@ -553,7 +555,7 @@ class __$PartyRoomStateCopyWithImpl<$Res>
/// Create a copy of PartyRoomState
/// with the given fields replaced by the non-null parameter values.
@override @pragma('vm:prefer-inline') $Res call({Object? currentRoom = freezed,Object? members = null,Object? tags = null,Object? signalTypes = null,Object? isInRoom = null,Object? isOwner = null,Object? roomUuid = freezed,Object? recentEvents = null,}) {
@override @pragma('vm:prefer-inline') $Res call({Object? currentRoom = freezed,Object? members = null,Object? tags = null,Object? signalTypes = null,Object? isInRoom = null,Object? isOwner = null,Object? roomUuid = freezed,Object? recentEvents = null,Object? eventStreamDisconnected = null,}) {
return _then(_PartyRoomState(
currentRoom: freezed == currentRoom ? _self.currentRoom : currentRoom // ignore: cast_nullable_to_non_nullable
as partroom.RoomInfo?,members: null == members ? _self._members : members // ignore: cast_nullable_to_non_nullable
@@ -563,7 +565,8 @@ as Map<String, common.SignalType>,isInRoom: null == isInRoom ? _self.isInRoom :
as bool,isOwner: null == isOwner ? _self.isOwner : isOwner // ignore: cast_nullable_to_non_nullable
as bool,roomUuid: freezed == roomUuid ? _self.roomUuid : roomUuid // ignore: cast_nullable_to_non_nullable
as String?,recentEvents: null == recentEvents ? _self._recentEvents : recentEvents // ignore: cast_nullable_to_non_nullable
as List<partroom.RoomEvent>,
as List<partroom.RoomEvent>,eventStreamDisconnected: null == eventStreamDisconnected ? _self.eventStreamDisconnected : eventStreamDisconnected // ignore: cast_nullable_to_non_nullable
as bool,
));
}

View File

@@ -44,7 +44,7 @@ final class PartyRoomProvider
}
}
String _$partyRoomHash() => r'd7182854c8caf5bb362c45a4e6e2ab40c2ef1b09';
String _$partyRoomHash() => r'5640c173d0820c681f3bc68872a2ab4f2fa29285';
/// PartyRoom Provider