123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- 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<RTCRoomManager>, 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<RTCCreatRoomSuccessEvent>(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<RTCRoomInfo>(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.ShowRoom)
- {
- Debug.Log(" GETWindow Scenes " + ScenesManager.Instance.getWindow());
- // 退出房间
- 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
- {
- /// <summary>
- /// 离线
- /// </summary>
- OFF,
- /// <summary>
- /// 当前只有眼镜端在房间
- /// </summary>
- Online,
- /// <summary>
- /// 眼镜端和专家端都在房间
- /// </summary>
- ExpertOnline,
- /// <summary>
- /// 眼镜端和控制台都在房间
- /// </summary>
- ConterlOnline,
- /// <summary>
- /// 眼镜、专家、控制台 都在房间
- /// </summary>
- ALLOnline
- }
- public enum UserEnum
- {
- Player,
- Expert,
- Conterl
- }
|