123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- using System.Collections.Generic;
- using Blue;
- using LitJson;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using UnityEngine;
- public class RTCService : IRTCService
- {
- private RTCRoomInfo mRTCRoomInfo;
- public Dictionary<int, RTCUserInfo> mUserDic { get; private set; } = new Dictionary<int, RTCUserInfo>(); // 房间用户字典
- public void OnInit()
- {
- // 创建房间
- this.RegisterEvent<RTCCreatRoomSuccessEvent>(RTCCreatRoomSuccess);
- // 连接RTC
- this.RegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
- this.RegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
- // 加入房间
- this.RegisterEvent<JoinRoomSuccessEvent>(JoinRoomSuccess);
- // 其他用户加入房间
- this.RegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom);
- this.RegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
- }
- /// <summary>
- /// 根据Token创建房间
- /// </summary>
- public void CreatRoom()
- {
- CoroutineSystem.Instance.StartCoroutine(
- HttpTool.Instance.SendHttp(HttpActionLang.rtc_CreateRoom, "", message =>
- {
- JObject jobject = JObject.Parse(message);
- if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
- {
- this.SendEvent(new RTCCreatRoomSuccessEvent() { rtcRoomInfo = jobject["data"].ToString() });
- }
- }, "application/x-www-form-urlencoded"));
- }
- /// <summary>s
- /// 加入房间
- /// </summary>
- public void JoinRoom(string roomId)
- {
- JsonData data = new JsonData();
- data["roomId"] = roomId;
- CoroutineSystem.Instance.StartCoroutine(
- HttpTool.Instance.SendHttp(HttpActionLang.rtc_JoinRoom, data.ToJson(), message =>
- {
- JObject jobject = JObject.Parse(message);
- if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
- {
- this.SendEvent(new JoinRoomSuccessEvent() { rtcRoomInfo = jobject["data"].ToString() });
- }
- }));
- }
- /// <summary>
- /// 根据url连接rtc
- /// </summary>
- public void ConnectByRTCUrl(string URL)
- {
- GHZRtcManager.Instance.ConnectRoom("https://"+mRTCRoomInfo.host,mRTCRoomInfo.token);
- this.GetService<ISendLogService>().SendLog("Blue","RoomID:"+mRTCRoomInfo.roomId);
- this.GetService<ISendLogService>().SendLog("Blue","Host:"+mRTCRoomInfo.host);
- this.GetService<ISendLogService>().SendLog("Blue","Token:"+mRTCRoomInfo.token);
- //this.SendEvent<RTCConnectSuccessEvent>();
- //this.SendEvent<RTCConnectFailEvent>();
- }
- /// <summary>
- /// 退出房间
- /// </summary>
- public void LeaveRoom()
- {
- mUserDic.Clear();
- GHZRtcManager.Instance.DisconnectRoom();
- Debug.LogError($"离开房间");
- }
- /// <summary>
- /// 控制摄像头
- /// </summary>
- public void ActiveVideo(bool active)
- {
- GHZRtcManager.Instance.OnWebCam(active);
- }
- /// <summary>
- /// 控制麦克风
- /// </summary>
- public void ActiveAudio(bool active)
- {
- GHZRtcManager.Instance.OnMicrophone(active);
- Debug.LogError($"ActiveAudio:{active}");
- }
- /// <summary>
- /// 将远程视频流静音
- /// </summary>
- public void MuteRemoteVideoStream(string userID, bool active)
- {
- Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
- }
- /// <summary>
- /// 将远程音频流静音
- /// </summary>
- public void MuteRemoteAudioStream(string userID, bool active)
- {
- Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
- }
- /// <summary>
- /// 视频静音
- /// </summary>
- public void MuteVideo(bool active)
- {
- Debug.LogError($"视频静音:{active}");
- }
- /// <summary>
- /// 音频静音
- /// </summary>
- public void MuteAudio(bool active)
- {
- Debug.LogError($"音频静音{active}");
- }
- /// <summary>
- /// 打开本地视频
- /// </summary>
- public void LocalVideo(bool active)
- {
- Debug.LogError($"打开本地视频{active}");
- }
- /// <summary>
- /// 打开本地音频
- /// </summary>
- public void LoacalAudio(bool active)
- {
- Debug.LogError($"打开本地音频{active}");
- }
- #region 事件
- // 创建房间
- private void RTCCreatRoomSuccess(RTCCreatRoomSuccessEvent e)
- {
- mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(e.rtcRoomInfo);
- Debug.LogError($"创建房间成功,房间ID:{mRTCRoomInfo.roomId}");
- Debug.LogError($"地址链接:https://{mRTCRoomInfo.host}");
- Debug.LogError($"Token:{mRTCRoomInfo.token}");
- ConnectByRTCUrl(mRTCRoomInfo.host);
- }
- // 加入房间
- private void JoinRoomSuccess(JoinRoomSuccessEvent e)
- {
- mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(e.rtcRoomInfo);
- Debug.LogError($"创建房间成功,房间ID:{mRTCRoomInfo.roomId}");
- Debug.LogError($"地址链接:https://{mRTCRoomInfo.host}");
- Debug.LogError($"Token:{mRTCRoomInfo.token}");
- ConnectByRTCUrl(mRTCRoomInfo.host);
- }
- // 连接RTC
- private void RTCConnectSuccess(RTCConnectSuccessEvent e)
- {
- Debug.LogError($"RTC:RTCConnectSuccess");
- this.UnRegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
- }
- private void RTCConnectFail(RTCConnectFailEvent e)
- {
- Debug.LogError($"RTC:RTCConnectFail");
- this.UnRegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
- }
- // 其他用户
- private void OtherUserJoinRoom(OtherUserJoinRoomEvent e)
- {
- RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
- if (!mUserDic.ContainsKey(RTCUserInfo.UserID))
- {
- mUserDic.Add(RTCUserInfo.UserID, RTCUserInfo);
- }
- this.UnRegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom);
- }
- private void OtherLeaveRoom(OtherLeaveRoomEvent e)
- {
- RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
- if (mUserDic.ContainsKey(RTCUserInfo.UserID))
- {
- mUserDic.Remove(RTCUserInfo.UserID);
- }
- this.UnRegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
- }
- #endregion
- }
|