|
@@ -1,20 +1,15 @@
|
|
-using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
-using System.Text;
|
|
|
|
using Blue;
|
|
using Blue;
|
|
-using GHZLangChao;
|
|
|
|
using LitJson;
|
|
using LitJson;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine;
|
|
-using UnityEngine.Networking;
|
|
|
|
|
|
|
|
public class RTCService : IRTCService
|
|
public class RTCService : IRTCService
|
|
{
|
|
{
|
|
- private string mToken;
|
|
|
|
private RTCRoomInfo mRTCRoomInfo;
|
|
private RTCRoomInfo mRTCRoomInfo;
|
|
- private List<RTCUserInfo> mUserList =new List<RTCUserInfo>();
|
|
+ public Dictionary<int, RTCUserInfo> mUserDic { get; private set; } = new Dictionary<int, RTCUserInfo>();
|
|
- public Dictionary<int, RTCUserInfo> mUserDic{ get; private set; } = new Dictionary<int, RTCUserInfo>();
|
|
+
|
|
public void OnInit()
|
|
public void OnInit()
|
|
{
|
|
{
|
|
|
|
|
|
@@ -30,31 +25,38 @@ public class RTCService : IRTCService
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-
|
|
+
|
|
|
|
|
|
- public void CloseRTC()
|
|
+ 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 CreatRoom()
|
|
+ public void JoinRoom(int roomId)
|
|
{
|
|
{
|
|
-
|
|
+ JsonData data = new JsonData();
|
|
- GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.rtc_CreateRoom, "", (string message) => {
|
|
+ data["roomId"] = roomId;
|
|
- JObject jobject = JObject.Parse(message);
|
|
+ CoroutineSystem.Instance.StartCoroutine(
|
|
- if (jobject["code"].ToString() == "200")
|
|
+ HttpTool.Instance.SendHttp(HttpActionLang.rtc_JoinRoom, data.ToJson(), message =>
|
|
{
|
|
{
|
|
- message = jobject["data"].ToString();
|
|
+ JObject jobject = JObject.Parse(message);
|
|
- if (!string.IsNullOrWhiteSpace(message))
|
|
+ if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
|
|
{
|
|
{
|
|
- mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(message);
|
|
+ this.SendEvent(new JoinRoomSuccessEvent() { rtcRoomInfo = jobject["data"].ToString() });
|
|
- this.SendEvent(new RTCCreatRoomSuccessEvent() { rtcRoomInfo = mRTCRoomInfo });
|
|
|
|
}
|
|
}
|
|
- }
|
|
+ }));
|
|
- }, "application/x-www-form-urlencoded"));
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -66,33 +68,12 @@ public class RTCService : IRTCService
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- 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 });
|
|
|
|
- }
|
|
|
|
- }));
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void LeaveRoom()
|
|
public void LeaveRoom()
|
|
{
|
|
{
|
|
- mUserList.Clear();
|
|
|
|
mUserDic.Clear();
|
|
mUserDic.Clear();
|
|
Debug.LogError($"离开房间");
|
|
Debug.LogError($"离开房间");
|
|
}
|
|
}
|
|
@@ -166,16 +147,18 @@ public class RTCService : IRTCService
|
|
|
|
|
|
private void RTCCreatRoomSuccess(RTCCreatRoomSuccessEvent e)
|
|
private void RTCCreatRoomSuccess(RTCCreatRoomSuccessEvent e)
|
|
{
|
|
{
|
|
- Debug.LogError($"创建房间成功,房间ID:{e.rtcRoomInfo.roomId}");
|
|
+ mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(e.rtcRoomInfo);
|
|
|
|
+ Debug.LogError($"创建房间成功,房间ID:{mRTCRoomInfo.roomId}");
|
|
|
|
|
|
|
|
|
|
- JoinRoom(e.rtcRoomInfo.roomId);
|
|
+ JoinRoom(mRTCRoomInfo.roomId);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
private void JoinRoomSuccess(JoinRoomSuccessEvent e)
|
|
private void JoinRoomSuccess(JoinRoomSuccessEvent e)
|
|
{
|
|
{
|
|
- Debug.LogError($"加入房间成功,房间号:{e.rtcRoomInfo.roomId}");
|
|
+ mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(e.rtcRoomInfo);
|
|
|
|
+ Debug.LogError($"加入房间成功,房间号:{mRTCRoomInfo.roomId}");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -194,85 +177,20 @@ public class RTCService : IRTCService
|
|
private void OtherUserJoinRoom(OtherUserJoinRoomEvent e)
|
|
private void OtherUserJoinRoom(OtherUserJoinRoomEvent e)
|
|
{
|
|
{
|
|
RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
|
|
RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
|
|
- if(!mUserDic.ContainsKey(RTCUserInfo.UserID))
|
|
+ if (!mUserDic.ContainsKey(RTCUserInfo.UserID))
|
|
{
|
|
{
|
|
- mUserDic.Add(RTCUserInfo.UserID,RTCUserInfo);
|
|
+ mUserDic.Add(RTCUserInfo.UserID, RTCUserInfo);
|
|
- mUserList.Add(RTCUserInfo);
|
|
|
|
}
|
|
}
|
|
this.UnRegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom);
|
|
this.UnRegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom);
|
|
}
|
|
}
|
|
private void OtherLeaveRoom(OtherLeaveRoomEvent e)
|
|
private void OtherLeaveRoom(OtherLeaveRoomEvent e)
|
|
{
|
|
{
|
|
RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
|
|
RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
|
|
- if(mUserDic.ContainsKey(RTCUserInfo.UserID))
|
|
+ if (mUserDic.ContainsKey(RTCUserInfo.UserID))
|
|
{
|
|
{
|
|
mUserDic.Remove(RTCUserInfo.UserID);
|
|
mUserDic.Remove(RTCUserInfo.UserID);
|
|
- mUserList.Remove(RTCUserInfo);
|
|
|
|
}
|
|
}
|
|
this.UnRegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
|
|
this.UnRegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
|
|
}
|
|
}
|
|
#endregion
|
|
#endregion
|
|
-
|
|
|
|
- #region 协程
|
|
|
|
-
|
|
|
|
- private string message;
|
|
|
|
- public IEnumerator RTCCreateRoom()
|
|
|
|
- {
|
|
|
|
- UnityWebRequest webRequest = new UnityWebRequest(HttpAction.mEndustryURL+HttpAction.rtc_CreateRoom,"POST");
|
|
|
|
-
|
|
|
|
- webRequest.SetRequestHeader("Content-Type","application/x-www-form-urlencoded");
|
|
|
|
- webRequest.SetRequestHeader("Authorization",login.UserInfo.Instance.Token);
|
|
|
|
- webRequest.downloadHandler = new DownloadHandlerBuffer();
|
|
|
|
- yield return webRequest.SendWebRequest();
|
|
|
|
- if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
|
|
|
|
- Debug.LogError($"Error:{webRequest.error},DownloadHandler:{webRequest.downloadHandler.text}");
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
|
|
|
|
- {
|
|
|
|
- message = webRequest.downloadHandler.text;
|
|
|
|
- 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});
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }*/
|
|
|
|
-
|
|
|
|
- public IEnumerator RTCJoinRoom(string roomID)
|
|
|
|
- {
|
|
|
|
- UnityWebRequest webRequest = new UnityWebRequest(HttpAction.mEndustryURL+HttpAction.rtc_CreateRoom,"POST");
|
|
|
|
-
|
|
|
|
- webRequest.SetRequestHeader("Content-Type","application/x-www-form-urlencoded");
|
|
|
|
- webRequest.SetRequestHeader("Authorization",login.UserInfo.Instance.Token);
|
|
|
|
- byte[] bodyRaw = Encoding.UTF8.GetBytes(roomID);
|
|
|
|
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
|
|
|
|
- webRequest.downloadHandler = new DownloadHandlerBuffer();
|
|
|
|
- yield return webRequest.SendWebRequest();
|
|
|
|
- if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
|
|
|
|
- {
|
|
|
|
- Debug.LogError($"Error:{webRequest.error},DownloadHandler:{webRequest.downloadHandler.text}");
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
|
|
|
|
- {
|
|
|
|
- message = webRequest.downloadHandler.text;
|
|
|
|
- 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});
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }*/
|
|
|
|
- #endregion
|
|
|
|
}
|
|
}
|