RTCService.cs 8.3 KB

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