RTCRoomManager.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Blue;
  6. using Newtonsoft.Json;
  7. using LitJson;
  8. using Newtonsoft.Json.Linq;
  9. public class RTCRoomManager : MonoSingleton<RTCRoomManager>, IController
  10. {
  11. public ParticipantView expertView;
  12. public RawImage rawImage;
  13. private RTCRoomInfo mRTCRoomInfo;
  14. public QuestionEnum question;
  15. public RTCEnum rtcState;
  16. public string controlPhone;
  17. public JObject expertData;
  18. public JObject controlData;
  19. private void Start()
  20. {
  21. this.RegisterEvent<RTCCreatRoomSuccessEvent>(CreatRoomSuccess).UnRegisterWhenGameObjectDestroyed(gameObject);
  22. rtcState = RTCEnum.OFF;
  23. question = QuestionEnum.NetWork;
  24. }
  25. private void Update()
  26. {
  27. if (GHZRtcManager.Instance.isRoom)
  28. {
  29. GHZRtcManager.Instance.isRoom = false;
  30. OnLocalUserJoined();
  31. }
  32. }
  33. private void CreatRoomSuccess(RTCCreatRoomSuccessEvent e)
  34. {
  35. Debug.Log("DGJ =====> JoinRoomSuccess");
  36. mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(e.rtcRoomInfo);
  37. UILogManager.Instance.text2.text = mRTCRoomInfo.roomId.ToString();
  38. UICallManager.Instance.SetMQTTRoomId(mRTCRoomInfo.roomId.ToString());
  39. }
  40. public void OnLocalUserJoined()
  41. {
  42. UserJoined(UserEnum.Player);
  43. if (ScenesManager.Instance.getWindow()== ScenesManager.SceneType.ShowRTC) // 当前在RTC 频道内
  44. {
  45. JsonData data = new JsonData();
  46. data["roomId"] = mRTCRoomInfo.roomId.ToString();
  47. data["questionCategory"] = (int)question;
  48. Debug.Log("DGJ =====> " + mRTCRoomInfo.roomId);
  49. StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.apply, data.ToJson(), message =>
  50. {
  51. Debug.Log("DGJ ===> HttpActionLang.apply " + HttpActionLang.apply + " " + message);
  52. JObject jobject = JObject.Parse(message);
  53. if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
  54. {
  55. Debug.Log("DGJ ===> ");
  56. // ScenesManager.Instance.showWindow(ScenesManager.SceneType.ShowRoom); // 等待收到专家入场
  57. }
  58. }));
  59. }
  60. else
  61. {
  62. JsonData obj = new JsonData();
  63. obj["action"] = "invite";
  64. obj["rtcRoom"] = mRTCRoomInfo.roomId.ToString();
  65. obj["personnel"] = new JsonData();
  66. obj["personnel"]["id"] = MQTTManager.Instance.id;
  67. obj["personnel"]["name"] = MQTTManager.Instance._username;
  68. obj["personnel"]["resourcePoo1"] = MQTTManager.Instance.resourcePool;
  69. obj["personnel"]["room"] = MQTTManager.Instance.room;
  70. obj["personnel"]["phone"] = MQTTManager.Instance.phone;
  71. // 用MQTT 向中考发送邀请
  72. MQTTManager.Instance.PushMsg(MQTTManager.Instance.front +"online/"+controlPhone, obj.ToJson());
  73. }
  74. }
  75. public void ExpertJonin(string identity)
  76. {
  77. RTCManager.Instance.ShowViewRawImage(identity, rawImage);
  78. }
  79. public void UserJoined( UserEnum userEnum , JObject metadata = null)
  80. {
  81. // Debug.Log("DGJ ===> " + rtcState);
  82. switch (userEnum)
  83. {
  84. case UserEnum.Player:
  85. rtcState = RTCEnum.Online;
  86. Debug.Log(" DGJ ===> UserJoined 自己 ");
  87. break;
  88. case UserEnum.Expert:
  89. expertData = metadata;
  90. switch (rtcState)
  91. {
  92. case RTCEnum.OFF:
  93. case RTCEnum.Online:
  94. case RTCEnum.ExpertOnline:
  95. rtcState = RTCEnum.ExpertOnline;
  96. break;
  97. case RTCEnum.ConterlOnline:
  98. rtcState = RTCEnum.ALLOnline;
  99. break;
  100. case RTCEnum.ALLOnline:
  101. Debug.LogError("RTC 房间状态有误 " );
  102. break;
  103. default:
  104. break;
  105. }
  106. Debug.Log(" DGJ ===> UserJoined 专家 ");
  107. break;
  108. case UserEnum.Conterl:
  109. controlData = metadata;
  110. switch (rtcState)
  111. {
  112. case RTCEnum.OFF:
  113. case RTCEnum.Online:
  114. rtcState = RTCEnum.ConterlOnline;
  115. break;
  116. case RTCEnum.ExpertOnline:
  117. rtcState = RTCEnum.ALLOnline;
  118. break;
  119. case RTCEnum.ConterlOnline:
  120. rtcState = RTCEnum.ConterlOnline;
  121. break;
  122. case RTCEnum.ALLOnline:
  123. Debug.LogError("RTC 房间状态有误 ");
  124. break;
  125. default:
  126. break;
  127. }
  128. Debug.Log(" DGJ ===> UserJoined 中控 ");
  129. break;
  130. default:
  131. break;
  132. }
  133. Debug.Log("DGJ ===> " + rtcState);
  134. }
  135. public void UserOffline(string uid)
  136. {
  137. Participant participant = RTCManager.Instance.QuesUser(uid);
  138. if(expertData!=null&& participant.identity == expertData["name"].ToString() )
  139. {
  140. UserOffline(UserEnum.Expert);
  141. Debug.Log("DGJ ===> UserOffline 专家 "+ uid);
  142. }else if(controlData!=null && participant.identity == controlData["name"].ToString())
  143. {
  144. UserOffline(UserEnum.Conterl);
  145. Debug.Log("DGJ ===> UserOffline 中控 " + uid);
  146. }
  147. }
  148. public void UserOffline(UserEnum userEnum)
  149. {
  150. switch (userEnum)
  151. {
  152. case UserEnum.Player:
  153. rtcState = RTCEnum.OFF;
  154. break;
  155. case UserEnum.Expert:
  156. switch (rtcState)
  157. {
  158. case RTCEnum.OFF:
  159. case RTCEnum.Online:
  160. case RTCEnum.ExpertOnline:
  161. rtcState = RTCEnum.Online;
  162. break;
  163. case RTCEnum.ConterlOnline:
  164. Debug.LogError("RTC 房间状态有误 ");
  165. break;
  166. case RTCEnum.ALLOnline:
  167. rtcState = RTCEnum.ConterlOnline;
  168. break;
  169. default:
  170. break;
  171. }
  172. break;
  173. case UserEnum.Conterl:
  174. switch (rtcState)
  175. {
  176. case RTCEnum.OFF:
  177. case RTCEnum.Online:
  178. case RTCEnum.ConterlOnline:
  179. rtcState = RTCEnum.Online;
  180. break;
  181. case RTCEnum.ExpertOnline:
  182. Debug.LogError("RTC 房间状态有误 ");
  183. break;
  184. case RTCEnum.ALLOnline:
  185. rtcState = RTCEnum.ExpertOnline;
  186. break;
  187. default:
  188. break;
  189. }
  190. break;
  191. default:
  192. break;
  193. }
  194. if(rtcState != RTCEnum.OFF&& ScenesManager.Instance.getWindow() != ScenesManager.SceneType.ShowRoom)
  195. {
  196. Debug.Log(" GETWindow Scenes " + ScenesManager.Instance.getWindow());
  197. // 退出房间
  198. LeaveChannel();
  199. }
  200. }
  201. public void LeaveChannel()
  202. {
  203. Debug.LogError(" RTCRoomManager 退出房间");
  204. RTCManager.Instance.LeaveChannel();
  205. rtcState = RTCEnum.OFF;
  206. ClearRoomData();
  207. }
  208. private void ClearRoomData()
  209. {
  210. expertView = null;
  211. mRTCRoomInfo = null;
  212. controlPhone = "";
  213. question = QuestionEnum.NetWork;
  214. expertData = null;
  215. controlData = null;
  216. }
  217. }
  218. public enum RTCEnum
  219. {
  220. /// <summary>
  221. /// 离线
  222. /// </summary>
  223. OFF,
  224. /// <summary>
  225. /// 当前只有眼镜端在房间
  226. /// </summary>
  227. Online,
  228. /// <summary>
  229. /// 眼镜端和专家端都在房间
  230. /// </summary>
  231. ExpertOnline,
  232. /// <summary>
  233. /// 眼镜端和控制台都在房间
  234. /// </summary>
  235. ConterlOnline,
  236. /// <summary>
  237. /// 眼镜、专家、控制台 都在房间
  238. /// </summary>
  239. ALLOnline
  240. }
  241. public enum UserEnum
  242. {
  243. Player,
  244. Expert,
  245. Conterl
  246. }