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);
-
- this.RegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
- this.RegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
-
- this.RegisterEvent<JoinRoomSuccessEvent>(JoinRoomSuccess);
-
- this.RegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom);
- this.RegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
- }
-
-
-
- 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"));
- }
-
-
-
- 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() });
- }
- }));
- }
-
-
-
- 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);
-
-
- }
-
-
-
- public void LeaveRoom()
- {
- mUserDic.Clear();
- GHZRtcManager.Instance.DisconnectRoom();
- Debug.LogError($"离开房间");
- }
-
-
-
- public void ActiveVideo(bool active)
- {
- GHZRtcManager.Instance.OnWebCam(active);
- }
-
-
-
- public void ActiveAudio(bool active)
- {
- GHZRtcManager.Instance.OnMicrophone(active);
- Debug.LogError($"ActiveAudio:{active}");
- }
-
-
-
- public void MuteRemoteVideoStream(string userID, bool active)
- {
- Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
- }
-
-
-
- public void MuteRemoteAudioStream(string userID, bool active)
- {
- Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
- }
-
-
-
- public void MuteVideo(bool active)
- {
- Debug.LogError($"视频静音:{active}");
- }
-
-
-
- public void MuteAudio(bool active)
- {
- Debug.LogError($"音频静音{active}");
- }
-
-
-
- public void LocalVideo(bool active)
- {
- Debug.LogError($"打开本地视频{active}");
- }
-
-
-
- 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);
- }
-
- 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
- }
|