using System.Collections.Generic; using Blue; using Newtonsoft.Json; using UnityEngine; public class RTCService : IRTCService { private string mToken; private RTCRoomInfo mRTCRoomInfo; private List mUserList =new List(); private Dictionary mUserDic = new Dictionary(); public void OnInit() { // 获取 token this.RegisterEvent(RTCGetTokenSuccess); this.RegisterEvent(RTCGetTokenFail); // 创建房间 this.RegisterEvent(RTCCreatRoomSuccess); this.RegisterEvent(RTCCreatRoomFail); // 连接RTC this.RegisterEvent(RTCConnectSuccess); this.RegisterEvent(RTCConnectFail); // 加入房间 this.RegisterEvent(JoinRoomSuccess); this.RegisterEvent(JoinRoomFail); } /// /// 关闭RTC /// public void CloseRTC() { } /// /// 登录后获取账号Token /// public void GetToken() { mToken = "...token..."; this.SendEvent(new RTCGetTokenSuccessEvent() { token = mToken }); //this.SendEvent(); } /// /// 根据Token创建房间 /// public void CreatRoom() { var mRTCUrl = "...RTCURL..."; RTCUserInfo RTCUserInfo = new RTCUserInfo() { UserID = 123,UserName="我是123",Head=321,Audio = false,Video = false }; mUserDic.Add(RTCUserInfo.UserID,RTCUserInfo); mUserList.Add(RTCUserInfo); mRTCRoomInfo = new RTCRoomInfo() { RoomID=111111,RoomName="我是111111",UserList = mUserList }; string currentRoomInfo = JsonConvert.SerializeObject(mRTCRoomInfo); this.SendEvent(new RTCCreatRoomSuccessEvent() { RTCUrl = mRTCUrl, roomInfo = currentRoomInfo }); //this.SendEvent(); } /// /// 根据url连接rtc /// public void ConnectByRTCUrl(string URL) { this.SendEvent(); //this.SendEvent(); } /// s /// 加入房间 /// public void JoinRoom(int roomId) { this.SendEvent(); //this.SendEvent(); } /// /// 退出房间 /// public void LeaveRoom() { mUserList.Clear(); mUserDic.Clear(); Debug.LogError($"离开房间"); } /// /// 控制摄像头 /// public void ActiveVideo(bool active) { Debug.LogError($"ActiveCamera:{active}"); } /// /// 控制麦克风 /// public void ActiveAudio(bool active) { Debug.LogError($"ActiveCamera:{active}"); } /// /// 将远程视频流静音 /// public void MuteRemoteVideoStream(string userID, bool active) { Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}"); } /// /// 将远程音频流静音 /// public void MuteRemoteAudioStream(string userID, bool active) { Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}"); } /// /// 视频静音 /// public void MuteVideo(bool active) { Debug.LogError($"视频静音:{active}"); } /// /// 音频静音 /// public void MuteAudio(bool active) { Debug.LogError($"音频静音{active}"); } /// /// 打开本地视频 /// public void LocalVideo(bool active) { Debug.LogError($"打开本地视频{active}"); } /// /// 打开本地音频 /// public void LoacalAudio(bool active) { Debug.LogError($"打开本地音频{active}"); } #region 事件 // 获取 token private void RTCGetTokenSuccess(RTCGetTokenSuccessEvent e) { Debug.LogError($"获取token成功:{e.token}"); this.UnRegisterEvent(RTCGetTokenSuccess); } private void RTCGetTokenFail(RTCGetTokenFailEvent e) { Debug.LogError($"RTC:RTCGetTokenFail"); this.UnRegisterEvent(RTCGetTokenFail); } // 创建房间 private void RTCCreatRoomSuccess(RTCCreatRoomSuccessEvent e) { Debug.LogError($"创建房间成功:{e.roomInfo}"); ConnectByRTCUrl(e.RTCUrl); this.UnRegisterEvent(RTCCreatRoomSuccess); } private void RTCCreatRoomFail(RTCCreatRoomFailEvent e) { Debug.LogError($"RTC:RTCGetTokenFail"); this.UnRegisterEvent(RTCCreatRoomFail); } // 连接RTC private void RTCConnectSuccess(RTCConnectSuccessEvent e) { Debug.LogError($"RTC:RTCConnectSuccess"); this.UnRegisterEvent(RTCConnectSuccess); } private void RTCConnectFail(RTCConnectFailEvent e) { Debug.LogError($"RTC:RTCConnectFail"); this.UnRegisterEvent(RTCConnectFail); } // 加入房间 private void JoinRoomSuccess(JoinRoomSuccessEvent e) { mUserList = JsonConvert.DeserializeObject>(e.roomInfo); foreach(RTCUserInfo user in mUserList) mUserDic.Add(user.UserID,user); Debug.LogError($"RTC:JoinRoomSuccess"); this.UnRegisterEvent(JoinRoomSuccess); } private void JoinRoomFail(JoinRoomFailEvent e) { Debug.LogError($"RTC:JoinRoomFail"); this.UnRegisterEvent(JoinRoomFail); } #endregion }