123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using Blue;
- using GHZLangChao;
- using LitJson;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using UnityEngine;
- using UnityEngine.Networking;
- public class RTCService : IRTCService
- {
- private string mToken;
- private RTCRoomInfo mRTCRoomInfo;
- private List<RTCUserInfo> mUserList =new List<RTCUserInfo>();
- 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 CloseRTC()
- {
- }
-
-
-
- public void CreatRoom()
- {
-
- GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.rtc_CreateRoom, "", (string message) => {
- JObject jobject = JObject.Parse(message);
- if (jobject["code"].ToString() == "200")
- {
- message = jobject["data"].ToString();
- if (!string.IsNullOrWhiteSpace(message))
- {
- mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(message);
- this.SendEvent(new RTCCreatRoomSuccessEvent() { rtcRoomInfo = mRTCRoomInfo });
- }
- }
- }, "application/x-www-form-urlencoded"));
- }
-
-
-
- public void ConnectByRTCUrl(string URL)
- {
- this.SendEvent<RTCConnectSuccessEvent>();
-
- }
-
-
-
- public void JoinRoom(int roomId)
- {
-
- JsonData data = new JsonData();
- data["roomId"] = roomId;
- GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.rtc_CreateRoom, data.ToJson(), (string message) => {
- JObject jobject = JObject.Parse(message);
- if (jobject["code"].ToString() == "200" && !string.IsNullOrEmpty(jobject["data"].ToString()))
- {
- message = jobject["data"].ToString();
- mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(message);
- this.SendEvent(new JoinRoomSuccessEvent() { rtcRoomInfo = mRTCRoomInfo });
- }
- }, "application/x-www-form-urlencoded"));
- }
-
-
-
- public void LeaveRoom()
- {
- mUserList.Clear();
- mUserDic.Clear();
- Debug.LogError($"离开房间");
- }
-
-
-
- public void ActiveVideo(bool active)
- {
- Debug.LogError($"ActiveVideo:{active}");
- }
-
-
-
- public void ActiveAudio(bool 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)
- {
- Debug.LogError($"创建房间成功,房间ID:{e.rtcRoomInfo.roomId}");
-
- JoinRoom(e.rtcRoomInfo.roomId);
- }
-
- private void JoinRoomSuccess(JoinRoomSuccessEvent e)
- {
- Debug.LogError($"加入房间成功,房间号:{e.rtcRoomInfo.roomId}");
- }
-
- 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);
- mUserList.Add(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);
- mUserList.Remove(RTCUserInfo);
- }
- this.UnRegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
- }
- #endregion
- #region 协程
-
-
- #endregion
- }
|