using LitJson; using Newtonsoft.Json; using ShadowStudio.Model; using ShadowStudio.UI; using System.Collections; using System.Collections.Generic; using UnityEngine; using XRTool.Util; public class BroadcastGoods//广播进行同步 { public bool request; public int id; public string method; } public class BroadcastGood//创建物体 { public bool request; public int id; public string method; } public class JoinReturnGoods { public bool request; public int id; public bool ok; public JoinData data; } public class JoinData { public List scenes; public int currentScene; public string peerId; } public struct SendData { public bool request; public int id; public string method; public Datas data; } public struct Datas { public int sceneId; public string goodsId; public string action; public string requesttime; public string sign; public string user_token; public string room_id; } public class WSRoomHandler { public BroadcastGoods broadcastGoods = new BroadcastGoods(); public JoinReturnGoods joinReturnGoods = new JoinReturnGoods(); public delegate void OnInitDelegate(JsonData data); public delegate void OnOtherUserJoinRoomDelegate(JsonData data); public delegate void OnOtherUserLeaveRoomDelegate(JsonData data); public delegate void OnMessageForwardDelegate(JsonData data); public delegate void OnListenCreateGoodDelegate(JsonData data); public delegate void OnListenDeleteGoodDelegate(JsonData data); public delegate void OnListenBroadcastGoodDelegate(JsonData data); public delegate void OnListenMessageForwardDelegate(JsonData data); public delegate void OnChangeSceneDelegate(string peerId, List scenes, int currentScene); public delegate void OnCloseDelegate(JsonData data); public OnInitDelegate OnInit; public OnOtherUserJoinRoomDelegate OnOtherUserJoinRoom; public OnOtherUserLeaveRoomDelegate OnOtherUserLeaveRoom; public OnMessageForwardDelegate OnMessageForward; public OnListenCreateGoodDelegate OnListenCreateGood; public OnListenDeleteGoodDelegate OnListenDeleteGood; public OnListenBroadcastGoodDelegate OnListenBroadcastGood; public OnListenMessageForwardDelegate OnListenMessageForward; public OnChangeSceneDelegate onChangeScene; public OnCloseDelegate OnClose; private Dictionary idList = new Dictionary(); private SendData sendData = new SendData(); private Datas datas = new Datas(); JsonData SendData = new JsonData(); public void SendRoomData(string method, Datas data) { sendData.request = true; sendData.id = Random.Range(10000000, 99999999); sendData.method = method; sendData.data = data; string message = JsonConvert.SerializeObject(sendData); CommonMethod.ShowNetLoading(WSHandler._roomClient.name,"房间通讯中..."); WSHandler.SendRoomMessage(message); } public Datas Returndats(int sceneId, string goodsId="") { datas.sceneId = sceneId; datas.goodsId = goodsId; return datas; } public void JoinRoom(string id, string pwd) { JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = Random.Range(10000000, 99999999); message["method"] = "joinRoom"; data["roomId"] = id; data["roomPwd"] = pwd; message["data"] = data; idList.Add(message["id"].ToString(), message["method"].ToString()); CommonMethod.ShowNetLoading(WSHandler._roomClient.name,"加入房间中..."); WSHandler.SendRoomMessage(message); } public void ExitRoom() { JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = Random.Range(10000000, 99999999); message["method"] = "leaveRoom"; message["data"] = data; CommonMethod.ShowNetLoading(WSHandler._roomClient.name,"退出房间中..."); WSHandler.SendRoomMessage(message); } /// /// 创建场景 /// public void CreateScene() { SendRoomData("createScene", datas); } /// /// 切换场景 /// /// public void ChangeScene(int sceneId) { SendRoomData("changeScene", Returndats(sceneId)); } /// /// 删除场景 /// /// public void DeleteScene(int sceneId) { SendRoomData("deleteScene", Returndats(sceneId)); } /// /// 物体场景切换 /// /// /// public void GoodsModiScene(int sceneId,string goodsId) { SendRoomData("goodsModiScene", Returndats(sceneId, goodsId)); } /// /// 物体存档 /// /// /// public void Archiving(int sceneId, string goodsId) { SendRoomData("archiving", datas); } public void OnMessageReceived(JsonData data, string message) { if (data.Keys.Contains("response")) { CommonMethod.HideNetLoading(WSHandler._roomClient.name); } if (data.Keys.Contains("errorCode")) { CommonMethod.HideLoading(); CommonMethod.ShowTextTusi(); if (data.Keys.Contains("errorReason")) { } else { } return; } if (data.Keys.Contains("method")) { switch (data["method"].ToString()) { case "connect": InitReceived(data); WSHandler.roomRtcinit(WSHandler._roomid); break; case "joinRoom"://其他用户加入房间 OtherUserJoinRoomReceived(data); break; case "leaveRoom"://其他用户离开房间 OtherUserLeaveRoomReceived(data); break; case "close"://账号在其他地方登陆 CloseReceived(data); break; case "changeScene": JoinReturnGoods returnGoods = JsonConvert.DeserializeObject(message); if (returnGoods != null) { ChangeSceneReceived(returnGoods.data.peerId, returnGoods.data.scenes, returnGoods.data.currentScene); } break; default: break; } } if (data.Keys.Contains("id")) { if (idList.ContainsKey(data["id"].ToString())) { switch (idList[data["id"].ToString()]) { case "joinRoom"://获取已经在房间的用户信息 JoinReturnGoods returnGoods = JsonConvert.DeserializeObject(message); break; default: break; } idList.Remove(data["id"].ToString()); } } } private void InitReceived(JsonData data) { if (OnInit != null) { OnInit(data); } } private void OtherUserJoinRoomReceived(JsonData data) { if (OnOtherUserJoinRoom != null) { OnOtherUserJoinRoom(data); } } private void OtherUserLeaveRoomReceived(JsonData data) { if (OnOtherUserLeaveRoom != null) { OnOtherUserLeaveRoom(data); } } public void ChangeSceneReceived(string peerId, List scenes, int currentScene) { onChangeScene?.Invoke(peerId, scenes, currentScene); } public void CloseReceived(JsonData data) { OnClose?.Invoke(data); } }