RTCService.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System.Collections.Generic;
  2. using Blue;
  3. using LitJson;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using UnityEngine;
  7. public class RTCService : IRTCService
  8. {
  9. private RTCRoomInfo mRTCRoomInfo;
  10. public Dictionary<int, RTCUserInfo> mUserDic { get; private set; } = new Dictionary<int, RTCUserInfo>(); // 房间用户字典
  11. public void OnInit()
  12. {
  13. // 创建房间
  14. this.RegisterEvent<RTCCreatRoomSuccessEvent>(RTCCreatRoomSuccess);
  15. // 连接RTC
  16. this.RegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
  17. this.RegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
  18. // 加入房间
  19. this.RegisterEvent<JoinRoomSuccessEvent>(JoinRoomSuccess);
  20. // 其他用户加入房间
  21. this.RegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom);
  22. this.RegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
  23. }
  24. /// <summary>
  25. /// 根据Token创建房间
  26. /// </summary>
  27. public void CreatRoom()
  28. {
  29. CoroutineSystem.Instance.StartCoroutine(
  30. HttpTool.Instance.SendHttp(HttpActionLang.rtc_CreateRoom, "", message =>
  31. {
  32. JObject jobject = JObject.Parse(message);
  33. if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
  34. {
  35. this.SendEvent(new RTCCreatRoomSuccessEvent() { rtcRoomInfo = jobject["data"].ToString() });
  36. }
  37. }, "application/x-www-form-urlencoded"));
  38. }
  39. /// <summary>s
  40. /// 加入房间
  41. /// </summary>
  42. public void JoinRoom(int roomId)
  43. {
  44. JsonData data = new JsonData();
  45. data["roomId"] = roomId;
  46. CoroutineSystem.Instance.StartCoroutine(
  47. HttpTool.Instance.SendHttp(HttpActionLang.rtc_JoinRoom, data.ToJson(), message =>
  48. {
  49. JObject jobject = JObject.Parse(message);
  50. if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
  51. {
  52. this.SendEvent(new JoinRoomSuccessEvent() { rtcRoomInfo = jobject["data"].ToString() });
  53. }
  54. }));
  55. }
  56. /// <summary>
  57. /// 根据url连接rtc
  58. /// </summary>
  59. public void ConnectByRTCUrl(string URL)
  60. {
  61. this.SendEvent<RTCConnectSuccessEvent>();
  62. //this.SendEvent<RTCConnectFailEvent>();
  63. }
  64. /// <summary>
  65. /// 退出房间
  66. /// </summary>
  67. public void LeaveRoom()
  68. {
  69. mUserDic.Clear();
  70. Debug.LogError($"离开房间");
  71. }
  72. /// <summary>
  73. /// 控制摄像头
  74. /// </summary>
  75. public void ActiveVideo(bool active)
  76. {
  77. Debug.LogError($"ActiveVideo:{active}");
  78. }
  79. /// <summary>
  80. /// 控制麦克风
  81. /// </summary>
  82. public void ActiveAudio(bool active)
  83. {
  84. Debug.LogError($"ActiveAudio:{active}");
  85. }
  86. /// <summary>
  87. /// 将远程视频流静音
  88. /// </summary>
  89. public void MuteRemoteVideoStream(string userID, bool active)
  90. {
  91. Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
  92. }
  93. /// <summary>
  94. /// 将远程音频流静音
  95. /// </summary>
  96. public void MuteRemoteAudioStream(string userID, bool active)
  97. {
  98. Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
  99. }
  100. /// <summary>
  101. /// 视频静音
  102. /// </summary>
  103. public void MuteVideo(bool active)
  104. {
  105. Debug.LogError($"视频静音:{active}");
  106. }
  107. /// <summary>
  108. /// 音频静音
  109. /// </summary>
  110. public void MuteAudio(bool active)
  111. {
  112. Debug.LogError($"音频静音{active}");
  113. }
  114. /// <summary>
  115. /// 打开本地视频
  116. /// </summary>
  117. public void LocalVideo(bool active)
  118. {
  119. Debug.LogError($"打开本地视频{active}");
  120. }
  121. /// <summary>
  122. /// 打开本地音频
  123. /// </summary>
  124. public void LoacalAudio(bool active)
  125. {
  126. Debug.LogError($"打开本地音频{active}");
  127. }
  128. #region 事件
  129. // 创建房间
  130. private void RTCCreatRoomSuccess(RTCCreatRoomSuccessEvent e)
  131. {
  132. mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(e.rtcRoomInfo);
  133. Debug.LogError($"创建房间成功,房间ID:{mRTCRoomInfo.roomId}");
  134. Debug.LogError($"地址链接:https://{mRTCRoomInfo.host}");
  135. Debug.LogError($"Token:{mRTCRoomInfo.token}");
  136. //ConnectByRTCUrl(e.rtcRoomInfo.host);
  137. }
  138. // 加入房间
  139. private void JoinRoomSuccess(JoinRoomSuccessEvent e)
  140. {
  141. mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(e.rtcRoomInfo);
  142. Debug.LogError($"创建房间成功,房间ID:{mRTCRoomInfo.roomId}");
  143. Debug.LogError($"地址链接:https://{mRTCRoomInfo.host}");
  144. Debug.LogError($"Token:{mRTCRoomInfo.token}");
  145. }
  146. // 连接RTC
  147. private void RTCConnectSuccess(RTCConnectSuccessEvent e)
  148. {
  149. Debug.LogError($"RTC:RTCConnectSuccess");
  150. this.UnRegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
  151. }
  152. private void RTCConnectFail(RTCConnectFailEvent e)
  153. {
  154. Debug.LogError($"RTC:RTCConnectFail");
  155. this.UnRegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
  156. }
  157. // 其他用户
  158. private void OtherUserJoinRoom(OtherUserJoinRoomEvent e)
  159. {
  160. RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
  161. if (!mUserDic.ContainsKey(RTCUserInfo.UserID))
  162. {
  163. mUserDic.Add(RTCUserInfo.UserID, RTCUserInfo);
  164. }
  165. this.UnRegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom);
  166. }
  167. private void OtherLeaveRoom(OtherLeaveRoomEvent e)
  168. {
  169. RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
  170. if (mUserDic.ContainsKey(RTCUserInfo.UserID))
  171. {
  172. mUserDic.Remove(RTCUserInfo.UserID);
  173. }
  174. this.UnRegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
  175. }
  176. #endregion
  177. }