RTCService.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Blue;
  5. using GHZLangChao;
  6. using LitJson;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. using UnityEngine;
  10. using UnityEngine.Networking;
  11. public class RTCService : IRTCService
  12. {
  13. private string mToken;
  14. private RTCRoomInfo mRTCRoomInfo;
  15. private List<RTCUserInfo> mUserList =new List<RTCUserInfo>(); // 房间用户列表
  16. public Dictionary<int, RTCUserInfo> mUserDic{ get; private set; } = new Dictionary<int, RTCUserInfo>(); // 房间用户字典
  17. public void OnInit()
  18. {
  19. // 创建房间
  20. this.RegisterEvent<RTCCreatRoomSuccessEvent>(RTCCreatRoomSuccess);
  21. // 连接RTC
  22. this.RegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
  23. this.RegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
  24. // 加入房间
  25. this.RegisterEvent<JoinRoomSuccessEvent>(JoinRoomSuccess);
  26. // 其他用户加入房间
  27. this.RegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom);
  28. this.RegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
  29. }
  30. /// <summary>
  31. /// 关闭RTC
  32. /// </summary>
  33. public void CloseRTC()
  34. {
  35. }
  36. /// <summary>
  37. /// 根据Token创建房间
  38. /// </summary>
  39. public void CreatRoom()
  40. {
  41. //CoroutineSystem.Instance.StartCoroutine(RTCCreateRoom());
  42. GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.rtc_CreateRoom, "", (string message) => {
  43. JObject jobject = JObject.Parse(message);
  44. if (jobject["code"].ToString() == "200")
  45. {
  46. message = jobject["data"].ToString();
  47. if (!string.IsNullOrWhiteSpace(message))
  48. {
  49. mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(message);
  50. this.SendEvent(new RTCCreatRoomSuccessEvent() { rtcRoomInfo = mRTCRoomInfo });
  51. }
  52. }
  53. }, "application/x-www-form-urlencoded"));
  54. }
  55. /// <summary>
  56. /// 根据url连接rtc
  57. /// </summary>
  58. public void ConnectByRTCUrl(string URL)
  59. {
  60. this.SendEvent<RTCConnectSuccessEvent>();
  61. //this.SendEvent<RTCConnectFailEvent>();
  62. }
  63. /// <summary>s
  64. /// 加入房间
  65. /// </summary>
  66. public void JoinRoom(int roomId)
  67. {
  68. // CoroutineSystem.Instance.StartCoroutine(RTCJoinRoom(roomId.ToString()));
  69. JsonData data = new JsonData();
  70. data["roomId"] = roomId;
  71. GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.rtc_CreateRoom, data.ToJson(), (string message) => {
  72. JObject jobject = JObject.Parse(message);
  73. if (jobject["code"].ToString() == "200" && !string.IsNullOrEmpty(jobject["data"].ToString()))
  74. {
  75. message = jobject["data"].ToString();
  76. mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(message);
  77. this.SendEvent(new JoinRoomSuccessEvent() { rtcRoomInfo = mRTCRoomInfo });
  78. }
  79. }, "application/x-www-form-urlencoded"));
  80. }
  81. /// <summary>
  82. /// 退出房间
  83. /// </summary>
  84. public void LeaveRoom()
  85. {
  86. mUserList.Clear();
  87. mUserDic.Clear();
  88. Debug.LogError($"离开房间");
  89. }
  90. /// <summary>
  91. /// 控制摄像头
  92. /// </summary>
  93. public void ActiveVideo(bool active)
  94. {
  95. Debug.LogError($"ActiveVideo:{active}");
  96. }
  97. /// <summary>
  98. /// 控制麦克风
  99. /// </summary>
  100. public void ActiveAudio(bool active)
  101. {
  102. Debug.LogError($"ActiveAudio:{active}");
  103. }
  104. /// <summary>
  105. /// 将远程视频流静音
  106. /// </summary>
  107. public void MuteRemoteVideoStream(string userID, bool active)
  108. {
  109. Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
  110. }
  111. /// <summary>
  112. /// 将远程音频流静音
  113. /// </summary>
  114. public void MuteRemoteAudioStream(string userID, bool active)
  115. {
  116. Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
  117. }
  118. /// <summary>
  119. /// 视频静音
  120. /// </summary>
  121. public void MuteVideo(bool active)
  122. {
  123. Debug.LogError($"视频静音:{active}");
  124. }
  125. /// <summary>
  126. /// 音频静音
  127. /// </summary>
  128. public void MuteAudio(bool active)
  129. {
  130. Debug.LogError($"音频静音{active}");
  131. }
  132. /// <summary>
  133. /// 打开本地视频
  134. /// </summary>
  135. public void LocalVideo(bool active)
  136. {
  137. Debug.LogError($"打开本地视频{active}");
  138. }
  139. /// <summary>
  140. /// 打开本地音频
  141. /// </summary>
  142. public void LoacalAudio(bool active)
  143. {
  144. Debug.LogError($"打开本地音频{active}");
  145. }
  146. #region 事件
  147. // 创建房间
  148. private void RTCCreatRoomSuccess(RTCCreatRoomSuccessEvent e)
  149. {
  150. Debug.LogError($"创建房间成功,房间ID:{e.rtcRoomInfo.roomId}");
  151. //ConnectByRTCUrl(e.rtcRoomInfo.host);
  152. JoinRoom(e.rtcRoomInfo.roomId);
  153. }
  154. // 加入房间
  155. private void JoinRoomSuccess(JoinRoomSuccessEvent e)
  156. {
  157. Debug.LogError($"加入房间成功,房间号:{e.rtcRoomInfo.roomId}");
  158. }
  159. // 连接RTC
  160. private void RTCConnectSuccess(RTCConnectSuccessEvent e)
  161. {
  162. Debug.LogError($"RTC:RTCConnectSuccess");
  163. this.UnRegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
  164. }
  165. private void RTCConnectFail(RTCConnectFailEvent e)
  166. {
  167. Debug.LogError($"RTC:RTCConnectFail");
  168. this.UnRegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
  169. }
  170. // 其他用户
  171. private void OtherUserJoinRoom(OtherUserJoinRoomEvent e)
  172. {
  173. RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
  174. if(!mUserDic.ContainsKey(RTCUserInfo.UserID))
  175. {
  176. mUserDic.Add(RTCUserInfo.UserID,RTCUserInfo);
  177. mUserList.Add(RTCUserInfo);
  178. }
  179. this.UnRegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom);
  180. }
  181. private void OtherLeaveRoom(OtherLeaveRoomEvent e)
  182. {
  183. RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
  184. if(mUserDic.ContainsKey(RTCUserInfo.UserID))
  185. {
  186. mUserDic.Remove(RTCUserInfo.UserID);
  187. mUserList.Remove(RTCUserInfo);
  188. }
  189. this.UnRegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
  190. }
  191. #endregion
  192. #region 协程
  193. /*
  194. private string message;
  195. public IEnumerator RTCCreateRoom()
  196. {
  197. UnityWebRequest webRequest = new UnityWebRequest(HttpAction.mEndustryURL+HttpAction.rtc_CreateRoom,"POST");
  198. //foreach( var head in HttpTool.Instance.RequestHeader) webRequest.SetRequestHeader(head.Key,head.Value); 暂时使用直接写的方式,等后续确定使用此行代码
  199. webRequest.SetRequestHeader("Content-Type","application/x-www-form-urlencoded");
  200. webRequest.SetRequestHeader("Authorization",login.UserInfo.Instance.Token);
  201. webRequest.downloadHandler = new DownloadHandlerBuffer();
  202. yield return webRequest.SendWebRequest();
  203. if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  204. Debug.LogError($"Error:{webRequest.error},DownloadHandler:{webRequest.downloadHandler.text}");
  205. else
  206. {
  207. if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
  208. {
  209. message = webRequest.downloadHandler.text;
  210. JObject jobject = JObject.Parse(message);
  211. if (jobject["code"].ToString() == "200")
  212. {
  213. message = jobject["data"].ToString();
  214. if (!string.IsNullOrWhiteSpace(message))
  215. {
  216. mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(message);
  217. this.SendEvent(new RTCCreatRoomSuccessEvent() { rtcRoomInfo = mRTCRoomInfo});
  218. }
  219. }
  220. }
  221. }
  222. }*/
  223. /*
  224. public IEnumerator RTCJoinRoom(string roomID)
  225. {
  226. UnityWebRequest webRequest = new UnityWebRequest(HttpAction.mEndustryURL+HttpAction.rtc_CreateRoom,"POST");
  227. //foreach( var head in HttpTool.Instance.RequestHeader) webRequest.SetRequestHeader(head.Key,head.Value); 暂时使用直接写的方式,等后续确定使用此行代码
  228. webRequest.SetRequestHeader("Content-Type","application/x-www-form-urlencoded");
  229. webRequest.SetRequestHeader("Authorization",login.UserInfo.Instance.Token);
  230. byte[] bodyRaw = Encoding.UTF8.GetBytes(roomID);
  231. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  232. webRequest.downloadHandler = new DownloadHandlerBuffer();
  233. yield return webRequest.SendWebRequest();
  234. if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  235. {
  236. Debug.LogError($"Error:{webRequest.error},DownloadHandler:{webRequest.downloadHandler.text}");
  237. }
  238. else
  239. {
  240. if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
  241. {
  242. message = webRequest.downloadHandler.text;
  243. JObject jobject = JObject.Parse(message);
  244. if (jobject["code"].ToString() == "200" && !string.IsNullOrEmpty(jobject["data"].ToString()))
  245. {
  246. message = jobject["data"].ToString();
  247. mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(message);
  248. this.SendEvent(new JoinRoomSuccessEvent() { rtcRoomInfo = mRTCRoomInfo});
  249. }
  250. }
  251. }
  252. }*/
  253. #endregion
  254. }