123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- using System.Collections.Generic;
- using Blue;
- using Newtonsoft.Json;
- using UnityEngine;
- 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<RTCGetTokenSuccessEvent>(RTCGetTokenSuccess);
- this.RegisterEvent<RTCGetTokenFailEvent>(RTCGetTokenFail);
-
- this.RegisterEvent<RTCCreatRoomSuccessEvent>(RTCCreatRoomSuccess);
- this.RegisterEvent<RTCCreatRoomFailEvent>(RTCCreatRoomFail);
-
- this.RegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
- this.RegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
-
- this.RegisterEvent<JoinRoomSuccessEvent>(JoinRoomSuccess);
- this.RegisterEvent<JoinRoomFailEvent>(JoinRoomFail);
-
- this.RegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom);
- this.RegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
- }
-
-
-
- public void CloseRTC()
- {
- }
-
-
-
- public void GetToken()
- {
- mToken = "...token...";
- this.SendEvent(new RTCGetTokenSuccessEvent() { token = mToken });
-
- }
-
-
-
- public void CreatRoom()
- {
- var mRTCUrl = "...RTCURL...";
- RTCUserInfo RTCUserInfo = new RTCUserInfo()
- {
- UserID = 123,UserName="我是123",Head=321,Audio = false,Video = false
- };
- mUserDic.Add(RTCUserInfo.UserID,RTCUserInfo);
- mUserList.Add(RTCUserInfo);
- mRTCRoomInfo = new RTCRoomInfo()
- {
- RoomID=111111,RoomName="我是111111",UserList = mUserList
- };
- string currentRoomInfo = JsonConvert.SerializeObject(mRTCRoomInfo);
- this.SendEvent(new RTCCreatRoomSuccessEvent() { RTCUrl = mRTCUrl, roomInfo = currentRoomInfo });
-
- }
-
-
-
- public void ConnectByRTCUrl(string URL)
- {
- this.SendEvent<RTCConnectSuccessEvent>();
-
- }
-
-
-
- public void JoinRoom(int roomId)
- {
-
- this.SendEvent<JoinRoomSuccessEvent>();
-
- }
-
-
-
- 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 RTCGetTokenSuccess(RTCGetTokenSuccessEvent e)
- {
- Debug.LogError($"获取token成功:{e.token}");
- this.UnRegisterEvent<RTCGetTokenSuccessEvent>(RTCGetTokenSuccess);
- }
- private void RTCGetTokenFail(RTCGetTokenFailEvent e)
- {
- Debug.LogError($"RTC:RTCGetTokenFail");
- this.UnRegisterEvent<RTCGetTokenFailEvent>(RTCGetTokenFail);
- }
-
- private void RTCCreatRoomSuccess(RTCCreatRoomSuccessEvent e)
- {
- Debug.LogError($"创建房间成功:{e.roomInfo}");
- ConnectByRTCUrl(e.RTCUrl);
- this.UnRegisterEvent<RTCCreatRoomSuccessEvent>(RTCCreatRoomSuccess);
- }
- private void RTCCreatRoomFail(RTCCreatRoomFailEvent e)
- {
- Debug.LogError($"RTC:RTCGetTokenFail");
- this.UnRegisterEvent<RTCCreatRoomFailEvent>(RTCCreatRoomFail);
- }
-
- 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 JoinRoomSuccess(JoinRoomSuccessEvent e)
- {
- mUserList = JsonConvert.DeserializeObject<List<RTCUserInfo>>(e.roomInfo);
- foreach(RTCUserInfo user in mUserList) mUserDic.Add(user.UserID,user);
- Debug.LogError($"RTC:JoinRoomSuccess");
- this.UnRegisterEvent<JoinRoomSuccessEvent>(JoinRoomSuccess);
- }
- private void JoinRoomFail(JoinRoomFailEvent e)
- {
- Debug.LogError($"RTC:JoinRoomFail");
- this.UnRegisterEvent<JoinRoomFailEvent>(JoinRoomFail);
- }
-
- 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
- }
|