using System.Collections.Generic; using Blue; using LitJson; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using UnityEngine; public class RTCService : IRTCService { private RTCRoomInfo mRTCRoomInfo; public Dictionary mUserDic { get; private set; } = new Dictionary(); // 房间用户字典 public void OnInit() { // 创建房间 this.RegisterEvent(RTCCreatRoomSuccess); // 连接RTC this.RegisterEvent(RTCConnectSuccess); this.RegisterEvent(RTCConnectFail); // 加入房间 this.RegisterEvent(JoinRoomSuccess); // 其他用户加入房间 this.RegisterEvent(OtherUserJoinRoom); this.RegisterEvent(OtherLeaveRoom); } /// /// 根据Token创建房间 /// public void CreatRoom() { CoroutineSystem.Instance.StartCoroutine( HttpTool.Instance.SendHttp(HttpActionLang.rtc_CreateRoom, "", message => { JObject jobject = JObject.Parse(message); if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString())) { this.SendEvent(new RTCCreatRoomSuccessEvent() { rtcRoomInfo = jobject["data"].ToString() }); } }, "application/x-www-form-urlencoded")); } /// s /// 加入房间 /// public void JoinRoom(string roomId) { JsonData data = new JsonData(); data["roomId"] = roomId; CoroutineSystem.Instance.StartCoroutine( HttpTool.Instance.SendHttp(HttpActionLang.rtc_JoinRoom, data.ToJson(), message => { JObject jobject = JObject.Parse(message); if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString())) { this.SendEvent(new JoinRoomSuccessEvent() { rtcRoomInfo = jobject["data"].ToString() }); } })); } /// /// 根据url连接rtc /// public void ConnectByRTCUrl(string URL) { GHZRtcManager.Instance.ConnectRoom("https://"+mRTCRoomInfo.host,mRTCRoomInfo.token); this.GetService().SendLog("Blue","RoomID:"+mRTCRoomInfo.roomId); this.GetService().SendLog("Blue","Host:"+mRTCRoomInfo.host); this.GetService().SendLog("Blue","Token:"+mRTCRoomInfo.token); //this.SendEvent(); //this.SendEvent(); } /// /// 退出房间 /// public void LeaveRoom() { mUserDic.Clear(); GHZRtcManager.Instance.DisconnectRoom(); Debug.LogError($"离开房间"); } /// /// 控制摄像头 /// public void ActiveVideo(bool active) { GHZRtcManager.Instance.OnWebCam(active); } /// /// 控制麦克风 /// public void ActiveAudio(bool active) { GHZRtcManager.Instance.OnMicrophone(active); Debug.LogError($"ActiveAudio:{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 事件 // 创建房间 private void RTCCreatRoomSuccess(RTCCreatRoomSuccessEvent e) { mRTCRoomInfo = JsonConvert.DeserializeObject(e.rtcRoomInfo); Debug.LogError($"创建房间成功,房间ID:{mRTCRoomInfo.roomId}"); Debug.LogError($"地址链接:https://{mRTCRoomInfo.host}"); Debug.LogError($"Token:{mRTCRoomInfo.token}"); ConnectByRTCUrl(mRTCRoomInfo.host); } // 加入房间 private void JoinRoomSuccess(JoinRoomSuccessEvent e) { mRTCRoomInfo = JsonConvert.DeserializeObject(e.rtcRoomInfo); Debug.LogError($"创建房间成功,房间ID:{mRTCRoomInfo.roomId}"); Debug.LogError($"地址链接:https://{mRTCRoomInfo.host}"); Debug.LogError($"Token:{mRTCRoomInfo.token}"); ConnectByRTCUrl(mRTCRoomInfo.host); } // 连接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 OtherUserJoinRoom(OtherUserJoinRoomEvent e) { RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject(e.rtcUserInfoJsonString); if (!mUserDic.ContainsKey(RTCUserInfo.UserID)) { mUserDic.Add(RTCUserInfo.UserID, RTCUserInfo); } this.UnRegisterEvent(OtherUserJoinRoom); } private void OtherLeaveRoom(OtherLeaveRoomEvent e) { RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject(e.rtcUserInfoJsonString); if (mUserDic.ContainsKey(RTCUserInfo.UserID)) { mUserDic.Remove(RTCUserInfo.UserID); } this.UnRegisterEvent(OtherLeaveRoom); } #endregion }