using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Blue; using Newtonsoft.Json; using LitJson; using Newtonsoft.Json.Linq; public class RTCRoomManager : MonoSingleton, IController { public ParticipantView expertView; public RawImage rawImage; private RTCRoomInfo mRTCRoomInfo; public QuestionEnum question; public RTCEnum rtcState; public string controlPhone; public JObject expertData; public JObject controlData; private void Start() { this.RegisterEvent(CreatRoomSuccess).UnRegisterWhenGameObjectDestroyed(gameObject); rtcState = RTCEnum.OFF; question = QuestionEnum.NetWork; } private void Update() { if (GHZRtcManager.Instance.isRoom) { GHZRtcManager.Instance.isRoom = false; OnLocalUserJoined(); } } private void CreatRoomSuccess(RTCCreatRoomSuccessEvent e) { Debug.Log("DGJ =====> JoinRoomSuccess"); mRTCRoomInfo = JsonConvert.DeserializeObject(e.rtcRoomInfo); UILogManager.Instance.text2.text = mRTCRoomInfo.roomId.ToString(); UICallManager.Instance.SetMQTTRoomId(mRTCRoomInfo.roomId.ToString()); } public void OnLocalUserJoined() { UserJoined(UserEnum.Player); if (ScenesManager.Instance.getWindow()== ScenesManager.SceneType.ShowRTC) // 当前在RTC 频道内 { JsonData data = new JsonData(); data["roomId"] = mRTCRoomInfo.roomId.ToString(); data["questionCategory"] = (int)question; Debug.Log("DGJ =====> " + mRTCRoomInfo.roomId); StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.apply, data.ToJson(), message => { Debug.Log("DGJ ===> HttpActionLang.apply " + HttpActionLang.apply + " " + message); JObject jobject = JObject.Parse(message); if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString())) { Debug.Log("DGJ ===> "); // ScenesManager.Instance.showWindow(ScenesManager.SceneType.ShowRoom); // 等待收到专家入场 } })); } else { JsonData obj = new JsonData(); obj["action"] = "invite"; obj["rtcRoom"] = mRTCRoomInfo.roomId.ToString(); obj["personnel"] = new JsonData(); obj["personnel"]["id"] = MQTTManager.Instance.id; obj["personnel"]["name"] = MQTTManager.Instance._username; obj["personnel"]["resourcePoo1"] = MQTTManager.Instance.resourcePool; obj["personnel"]["room"] = MQTTManager.Instance.room; obj["personnel"]["phone"] = MQTTManager.Instance.phone; // 用MQTT 向中考发送邀请 MQTTManager.Instance.PushMsg(MQTTManager.Instance.front +"online/"+controlPhone, obj.ToJson()); } } public void ExpertJonin(string identity) { RTCManager.Instance.ShowViewRawImage(identity, rawImage); } public void UserJoined( UserEnum userEnum , JObject metadata = null) { // Debug.Log("DGJ ===> " + rtcState); switch (userEnum) { case UserEnum.Player: rtcState = RTCEnum.Online; Debug.Log(" DGJ ===> UserJoined 自己 "); break; case UserEnum.Expert: expertData = metadata; switch (rtcState) { case RTCEnum.OFF: case RTCEnum.Online: case RTCEnum.ExpertOnline: rtcState = RTCEnum.ExpertOnline; break; case RTCEnum.ConterlOnline: rtcState = RTCEnum.ALLOnline; break; case RTCEnum.ALLOnline: Debug.LogError("RTC 房间状态有误 " ); break; default: break; } Debug.Log(" DGJ ===> UserJoined 专家 "); break; case UserEnum.Conterl: controlData = metadata; switch (rtcState) { case RTCEnum.OFF: case RTCEnum.Online: rtcState = RTCEnum.ConterlOnline; break; case RTCEnum.ExpertOnline: rtcState = RTCEnum.ALLOnline; break; case RTCEnum.ConterlOnline: rtcState = RTCEnum.ConterlOnline; break; case RTCEnum.ALLOnline: Debug.LogError("RTC 房间状态有误 "); break; default: break; } Debug.Log(" DGJ ===> UserJoined 中控 "); break; default: break; } Debug.Log("DGJ ===> " + rtcState); } public void UserOffline(string uid) { Participant participant = RTCManager.Instance.QuesUser(uid); if(expertData!=null&& participant.identity == expertData["name"].ToString() ) { UserOffline(UserEnum.Expert); Debug.Log("DGJ ===> UserOffline 专家 "+ uid); }else if(controlData!=null && participant.identity == controlData["name"].ToString()) { UserOffline(UserEnum.Conterl); Debug.Log("DGJ ===> UserOffline 中控 " + uid); } } public void UserOffline(UserEnum userEnum) { switch (userEnum) { case UserEnum.Player: rtcState = RTCEnum.OFF; break; case UserEnum.Expert: switch (rtcState) { case RTCEnum.OFF: case RTCEnum.Online: case RTCEnum.ExpertOnline: rtcState = RTCEnum.Online; break; case RTCEnum.ConterlOnline: Debug.LogError("RTC 房间状态有误 "); break; case RTCEnum.ALLOnline: rtcState = RTCEnum.ConterlOnline; break; default: break; } break; case UserEnum.Conterl: switch (rtcState) { case RTCEnum.OFF: case RTCEnum.Online: case RTCEnum.ConterlOnline: rtcState = RTCEnum.Online; break; case RTCEnum.ExpertOnline: Debug.LogError("RTC 房间状态有误 "); break; case RTCEnum.ALLOnline: rtcState = RTCEnum.ExpertOnline; break; default: break; } break; default: break; } if(rtcState != RTCEnum.OFF&& ScenesManager.Instance.getWindow() != ScenesManager.SceneType.ShowRTC) { // 退出房间 LeaveChannel(); } } public void LeaveChannel() { Debug.LogError(" RTCRoomManager 退出房间"); RTCManager.Instance.LeaveChannel(); rtcState = RTCEnum.OFF; ClearRoomData(); } private void ClearRoomData() { expertView = null; mRTCRoomInfo = null; controlPhone = ""; question = QuestionEnum.NetWork; expertData = null; controlData = null; } } public enum RTCEnum { /// /// 离线 /// OFF, /// /// 当前只有眼镜端在房间 /// Online, /// /// 眼镜端和专家端都在房间 /// ExpertOnline, /// /// 眼镜端和控制台都在房间 /// ConterlOnline, /// /// 眼镜、专家、控制台 都在房间 /// ALLOnline } public enum UserEnum { Player, Expert, Conterl }