Browse Source

refactor(rtc): 重构RTC频道详情获取逻辑

- 将_fetchRtcChannelDetail方法改为私有,并新增公共方法fetchRtcChannelDetail
- 在RTCManager中用户加入和离开频道时主动调用频道详情获取
- 移除RoomController中冗余的用户加入/离开消息处理逻辑
- 优化RTCManager中的网络服务依赖导入
- 调整_leaveChat消息发送逻辑以适配新的角色判断方式
ios
Jolie 4 months ago
parent
commit
7fd7c032fa
2 changed files with 52 additions and 94 deletions
  1. 89
      lib/controller/discover/room_controller.dart
  2. 57
      lib/rtc/rtc_manager.dart

89
lib/controller/discover/room_controller.dart

@ -264,7 +264,8 @@ class RoomController extends GetxController with WidgetsBindingObserver {
}
}
Future<void> _fetchRtcChannelDetail(String channelName) async {
/// RTC
Future<void> fetchRtcChannelDetail(String channelName) async {
try {
final response = await _networkService.rtcApi.getRtcChannelDetail(
channelName,
@ -278,6 +279,10 @@ class RoomController extends GetxController with WidgetsBindingObserver {
}
}
Future<void> _fetchRtcChannelDetail(String channelName) async {
await fetchRtcChannelDetail(channelName);
}
///
Future<void> sendChatMessage(String content) async {
final channelName = RTCManager.instance.currentChannelId;
@ -453,88 +458,6 @@ class RoomController extends GetxController with WidgetsBindingObserver {
} catch (e) {
print('❌ 处理礼物消息失败: $e');
}
} else if (message['type'] == 'join_chat') {
final response = await _networkService.rtcApi
.getDatingRtcChannelUserDetail(
rtcChannel.value!.channelId,
message['uid'],
);
if (!response.data.isSuccess) {
return;
}
final currentDetail = rtcChannelDetail.value;
if (currentDetail == null) {
return;
}
final userData = response.data.data;
if (userData == null) {
return;
}
if (message['role'] == 'male_audience') {
final maleInfo = RtcSeatUserInfo(
miId: userData.miId,
userId: userData.userId,
nickName: userData.nickName,
profilePhoto: userData.profilePhoto,
genderCode: userData.genderCode,
seatNumber: userData.seatNumber,
isFriend: userData.isFriend,
isMicrophoneOn: userData.isMicrophoneOn,
isVideoOn: userData.isVideoOn,
uid: message['uid'] is int
? message['uid'] as int
: int.tryParse(message['uid']?.toString() ?? ''),
);
final newDetail = RtcChannelDetail(
channelId: currentDetail.channelId,
anchorInfo: currentDetail.anchorInfo,
maleInfo: maleInfo,
femaleInfo: currentDetail.femaleInfo,
);
rtcChannelDetail.value = newDetail;
} else if (message['role'] == 'female_audience') {
final femaleInfo = RtcSeatUserInfo(
miId: userData.miId,
userId: userData.userId,
nickName: userData.nickName,
profilePhoto: userData.profilePhoto,
genderCode: userData.genderCode,
seatNumber: userData.seatNumber,
isFriend: userData.isFriend,
isMicrophoneOn: userData.isMicrophoneOn,
isVideoOn: userData.isVideoOn,
uid: message['uid'] is int
? message['uid'] as int
: int.tryParse(message['uid']?.toString() ?? ''),
);
final newDetail = RtcChannelDetail(
channelId: currentDetail.channelId,
anchorInfo: currentDetail.anchorInfo,
maleInfo: currentDetail.maleInfo,
femaleInfo: femaleInfo,
);
rtcChannelDetail.value = newDetail;
}
// }
} else if (message['type'] == 'leave_chat') {
if (message['role'] == 'male_audience') {
final newDetail = RtcChannelDetail(
channelId: rtcChannelDetail.value!.channelId,
anchorInfo: rtcChannelDetail.value!.anchorInfo,
maleInfo: null,
femaleInfo: rtcChannelDetail.value!.femaleInfo,
);
rtcChannelDetail.value = newDetail;
} else if (message['role'] == 'female_audience') {
final newDetail = RtcChannelDetail(
channelId: rtcChannelDetail.value!.channelId,
anchorInfo: rtcChannelDetail.value!.anchorInfo,
maleInfo: rtcChannelDetail.value!.maleInfo,
femaleInfo: null,
);
rtcChannelDetail.value = newDetail;
}
}
}

57
lib/rtc/rtc_manager.dart

@ -6,7 +6,6 @@ import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import '../controller/discover/room_controller.dart';
import '../network/network_service.dart';
import '../pages/discover/live_room_page.dart';
/// RTC
@ -119,6 +118,16 @@ class RTCManager {
channelJoinedNotifier.value = true;
_currentChannelId = connection.channelId;
print('加入频道成功,频道名:${connection.channelId},耗时:${elapsed}ms');
// RoomController fetchRtcChannelDetail
final channelId = connection.channelId;
if (Get.isRegistered<RoomController>() &&
channelId != null &&
channelId.isNotEmpty) {
final roomController = Get.find<RoomController>();
await roomController.fetchRtcChannelDetail(channelId);
}
if (connection.localUid == _currentUid) {
await RTMManager.instance.subscribe(_currentChannelId ?? '');
await RTMManager.instance.publishChannelMessage(
@ -131,21 +140,42 @@ class RTCManager {
onJoinChannelSuccess!(connection, elapsed);
}
},
onUserJoined: (RtcConnection connection, int remoteUid, int elapsed) async{
print('用户加入,UID:$remoteUid');
_handleRemoteUserJoined(remoteUid);
if (onUserJoined != null) {
onUserJoined!(connection, remoteUid, elapsed);
}
},
onUserJoined:
(RtcConnection connection, int remoteUid, int elapsed) async {
print('用户加入,UID:$remoteUid');
_handleRemoteUserJoined(remoteUid);
// RoomController fetchRtcChannelDetail
final channelId = connection.channelId;
if (Get.isRegistered<RoomController>() &&
channelId != null &&
channelId.isNotEmpty) {
final roomController = Get.find<RoomController>();
await roomController.fetchRtcChannelDetail(channelId);
}
if (onUserJoined != null) {
onUserJoined!(connection, remoteUid, elapsed);
}
},
onUserOffline:
(
RtcConnection connection,
int remoteUid,
UserOfflineReasonType reason,
) {
) async {
print('用户离开,UID:$remoteUid,原因:$reason');
_handleRemoteUserOffline(remoteUid);
// RoomController fetchRtcChannelDetail
final channelId = connection.channelId;
if (Get.isRegistered<RoomController>() &&
channelId != null &&
channelId.isNotEmpty) {
final roomController = Get.find<RoomController>();
await roomController.fetchRtcChannelDetail(channelId);
}
if (onUserOffline != null) {
onUserOffline!(connection, remoteUid, reason);
}
@ -521,8 +551,13 @@ class RTCManager {
await _engine?.muteLocalVideoStream(true);
await RTMManager.instance.publishChannelMessage(
channelName: _currentChannelId ?? '',
message: json.encode({'type': 'leave_chat', 'uid': _currentUid, 'role': role == CurrentRole.maleAudience
? 'male_audience' : 'female_audience',}),
message: json.encode({
'type': 'leave_chat',
'uid': _currentUid,
'role': role == CurrentRole.maleAudience
? 'male_audience'
: 'female_audience',
}),
);
}
}
Loading…
Cancel
Save