using LitJson; using Newtonsoft.Json; using ShadowStudio.Model; using ShadowStudio.UI; using Studio.WebSocket.Message; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using XRTool.Util; using Random = UnityEngine.Random; public class NotifyGoods { public bool notification = true; public string method = "transmitGoods"; public List data; } public class BroadcastGoods//广播进行同步 { public bool request; public int id; public string method; public List data; } public class BroadcastGood//创建物体 { public bool request; public int id; public string method; public GoodsInfo data; } public class JoinReturnGoods { public bool request; public int id; public bool ok; public JoinData data; } public class JoinData { public List goods; public List peers; public List scenes; public SceneConfig currentScene; public string peerId; } public struct SendData { public bool request; public int id; public string method; public SceneDatas 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 GoodsInfo good; } public struct SceneDatas { public int roomID; public string sceneName; public int sceneId; } public class WSRoomHandler { public BroadcastGoods broadcastGoods = new BroadcastGoods(); public NotifyGoods notifyGoods = new NotifyGoods(); public JoinReturnGoods joinReturnGoods = new JoinReturnGoods(); public delegate void OnInitDelegate(JsonData data); public delegate void OnJoinRoomDelegate(List goods, List peers, List scenes, SceneConfig currentScene); public delegate void OnOtherUserJoinRoomDelegate(JsonData data); public delegate void OnOtherUserLeaveRoomDelegate(JsonData data); public delegate void OnBroadcastGoodDelegate(List 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, SceneConfig currentScene); public delegate void OnCloseDelegate(JsonData data); public OnInitDelegate OnInit; public OnJoinRoomDelegate OnJoinRoom; public OnOtherUserJoinRoomDelegate OnOtherUserJoinRoom; public OnOtherUserLeaveRoomDelegate OnOtherUserLeaveRoom; public OnBroadcastGoodDelegate OnBroadcastGood; 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(); private SceneDatas sceneDatas = new SceneDatas(); JsonData SendData = new JsonData(); Dictionary> dicRequest = new Dictionary>(); public void SendRoomData(string method, SceneDatas data, Action callBack=null) { sendData.request = true; sendData.id = Random.Range(10000000, 99999999); sendData.method = method; sendData.data = data; string message = JsonConvert.SerializeObject(sendData); if (WSHandler._roomClient) { CommonMethod.ShowNetLoading(WSHandler._roomClient.name, "房间通讯中..."); } else { UnityLog.Instance.LogError("SendRoomData _roomClient is destroy!"); } if(callBack!=null) { dicRequest.Add(sendData.id, callBack); } WSHandler.SendRoomMessage(message); } public SceneDatas ReturnScennedats(int sceneId, string sceneName = "") { sceneDatas.roomID = int.Parse(ShowViewMgr.Instance.roomId); sceneDatas.sceneId = sceneId; sceneDatas.sceneName = sceneName; return sceneDatas; } public SceneDatas ReturnScennedats(string sceneName) { sceneDatas.roomID = int.Parse(ShowViewMgr.Instance.roomId); sceneDatas.sceneName = sceneName; return sceneDatas; } public Datas Returndats(GoodsInfo good) { datas.action = "20300"; datas.room_id = ShowViewMgr.Instance.roomId; datas.requesttime = CommonMethod.GetCurrentTimeUnix().ToString(); datas.user_token = UserInfo.User_Token; datas.good = good; datas.sign = "588cd263bfd7a7b5e561dba46b989ef1"; 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); // AgoraVideoAudioManager.Instance.JoinChannel(id); } 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 CreateGood(int aid,string art_id, string goods_name, string goods_info, string goods_extended, int status = 0) { string roomid = ""; if (ShowViewMgr.Instance != null) { roomid = ShowViewMgr.Instance.roomId; } CreateGood(aid,art_id, goods_name, goods_info, goods_extended, roomid, status); } public void CreateGood(int aid,string art_id, string goods_name, string goods_info, string goods_extended, string room_id, int status = 0) { if (WSHandler._roomClient) { JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = Random.Range(10000000, 99999999); message["method"] = "createGoods"; data["aid"] = aid; data["action"] = "20300"; data["art_id"] = art_id; data["goods_name"] = goods_name; data["goods_info"] = goods_info; data["goods_extended"] = goods_extended; data["room_id"] = room_id; data["status"] = status; data["scene_id"] = CommonMethod.currentScene.Id; data["requesttime"] = CommonMethod.GetCurrentTimeUnix().ToString(); data["user_token"] = UserInfo.User_Token; data["sign"] = "588cd263bfd7a7b5e561dba46b989ef1"; message["data"] = data; CommonMethod.ShowNetLoading(WSHandler._roomClient.name, "创建物体..."); WSHandler.SendRoomMessage(message); } } public void CreateGood(GoodsInfo goods) { if (ShowViewMgr.Instance) { //SendRoomData("createGoods", Returndats(goods));//这个是等服务器优化过数据结构后要用的 CreateGood(goods.rid,goods.art_id, goods.goods_name, goods.goods_info, goods.goods_extended, ShowViewMgr.Instance.roomId, goods.status); } } public void DeleteGood(int[] goods_id) { JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = Random.Range(10000000, 99999999); message["method"] = "deleteGoods"; data["action"] = "20303"; ///曹霆增加 2021/6/10 服务器要求增加(李圣辉) data["sceneID"] = CommonMethod.currentScene.Id; data["goods_ids"] = JsonMapper.ToObject(JsonConvert.SerializeObject(goods_id)); //data["goods_ids"].Add (goods_id); data["requesttime"] = CommonMethod.GetCurrentTimeUnix().ToString(); data["user_token"] = UserInfo.User_Token; data["sign"] = "588cd263bfd7a7b5e561dba46b989ef1"; message["data"] = data; CommonMethod.ShowNetLoading(WSHandler._roomClient.name, "删除物体..."); WSHandler.SendRoomMessage(message); } public void BroadcastGood(List goodsInfos, string method = "transmitGoods") { broadcastGoods.request = true; broadcastGoods.id = Random.Range(10000000, 99999999); broadcastGoods.method = method; broadcastGoods.data = goodsInfos; // CommonMethod.ShowNetLoading(WSHandler._roomClient.name); WSHandler.SendRoomMessage(JsonConvert.SerializeObject(broadcastGoods)); } public void BroadNotifycastGood(List goodsInfos, string method = "transmitGoods") { notifyGoods.data = goodsInfos; notifyGoods.method = method; WSHandler.SendRoomMessage(JsonConvert.SerializeObject(notifyGoods)); } /// /// 转发信息 /// /// public void TransmitGood(List goodsInfos) { BroadNotifycastGood(goodsInfos, "transmitGoods"); //BroadcastGood(goodsInfos, "transmit"); } /// /// 创建场景 /// public void CreateScene(string sceneName) { SendRoomData("createScene", ReturnScennedats(sceneName)); } /// /// 切换场景 /// /// public void ChangeScene(int sceneId) { SendRoomData("changeScene", ReturnScennedats(sceneId)); } /// /// 场景名字 /// /// public void ChangeSceneName(int sceneId, string sceneName) { SendRoomData("updateScene", ReturnScennedats(sceneId, sceneName)); } /// /// 删除场景 /// /// public void DeleteScene(int sceneId) { SendRoomData("deleteScene", ReturnScennedats(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) { //Debug.Log("data====>"+ data.ToJson()); if (data.Keys.Contains("response")) { CommonMethod.HideNetLoading(WSHandler._roomClient.name); if(dicRequest.ContainsKey(int.Parse(data["id"].ToString()))) { dicRequest[int.Parse(data["id"].ToString())].Invoke(data); dicRequest.Remove(int.Parse(data["id"].ToString())); } if(message.Contains("uid")) { Debug.Log(data["data"]["peers"][0]["peerId"].ToString()); Debug.Log((uint)Int32.Parse(data["data"]["peers"][0]["uid"].ToString())); AgoraRTCManager.Instance.AddDicPeerData(data["data"]["peers"][0]["peerId"].ToString(), (uint)Int32.Parse(data["data"]["peers"][0]["uid"].ToString())); } } if (data.Keys.Contains("errorCode")) { CommonMethod.HideLoading(); CommonMethod.ShowTextTusi(); if (TuSi.Instance) { if (data.Keys.Contains("errorReason")) { TuSi.Instance.ShowTextContent(data["errorReason"].ToString()); } else { TuSi.Instance.ShowTextContent(data["errorCode"]["message"].ToString()); string code = data["errorCode"]["code"].ToString(); if (code == "5089"|| code == "5091" || code == "5093" || code == "5094") { WSHandler.roomCloes(); } } } //被zby替换的代码 //if (RoomListDlg.Instance && RoomListDlg.Instance.roomlist) //{ // RoomListDlg.Instance.roomlist.RefeshisClick(); //} if (MenuListDlg.Instance && MenuListDlg.Instance._menuUI) { //当进入房间失败时,取消事件监听 MenuListDlg.Instance._menuUI.RefeshisClick(); } return; } if (data.Keys.Contains("method")) { switch (data["method"].ToString()) { case "transmitGoods": BroadcastGoods broadcast = JsonConvert.DeserializeObject(message); if (broadcast != null) { BroadcastGoodReceived(broadcast.data); } break; case "transmit": BroadcastGoods broad = JsonConvert.DeserializeObject(message); if (broad != null) { BroadcastGoodReceived(broad.data); } break; case "connect": InitReceived(data); break; case "joinRoom"://其他用户加入房间 OtherUserJoinRoomReceived(data); break; case "createGoods": CreateGoodReceived(message); break; case "deleteGoods": DeleteGoodReceived(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); if (returnGoods != null) { JoinRoomReceived(returnGoods.data.goods, returnGoods.data.peers, returnGoods.data.scenes, returnGoods.data.currentScene); } // WSHandler.roomRtcinit(); // 进入房间 break; default: break; } idList.Remove(data["id"].ToString()); } } } public void InitReceived(JsonData data) { if (OnInit != null) { OnInit(data); Debug.Log(data["data"]["uid"]); Debug.Log(data["data"]["peerId"]); AgoraRTCManager.Instance.Uid = (uint)Int32.Parse(data["data"]["uid"].ToString()); AgoraRTCManager.Instance.PeerId = data["data"]["peerId"].ToString(); AgoraRTCManager.Instance.AddDicPeerData(data["data"]["peerId"].ToString(), (uint)Int32.Parse(data["data"]["uid"].ToString())); } } private void JoinRoomReceived(List goods, List peers, List scenes, SceneConfig currentScene) { if (OnJoinRoom != null) { OnJoinRoom(goods, peers, scenes, currentScene); } } private void OtherUserJoinRoomReceived(JsonData data) { if (OnOtherUserJoinRoom != null) { OnOtherUserJoinRoom(data); AgoraRTCManager.Instance.AddDicPeerData(data["data"]["peerId"].ToString(), (uint)Int32.Parse(data["data"]["uid"].ToString())); } } private void OtherUserLeaveRoomReceived(JsonData data) { if (OnOtherUserLeaveRoom != null) { OnOtherUserLeaveRoom(data); AgoraRTCManager.Instance.RemDicPeerData(data["data"]["peerId"].ToString()); } } //接受服务器创建物体的消息 private void CreateGoodReceived(string message) { BroadcastGood broadcast = JsonConvert.DeserializeObject(message); if (broadcast != null) { ArtInfoMgr.Instance.SyncGoods(broadcast.data); } } private void DeleteGoodReceived(JsonData data) { List list = JsonConvert.DeserializeObject>(data["data"]["ids"].ToJson()); if (list != null) { for (int i = 0; i < list.Count; i++) { ArtInfoMgr.Instance.GoodsDelSync(list[i]); } } } public void BroadcastGoodReceived(List data) { if (OnBroadcastGood != null) { OnBroadcastGood(data); } } public void ChangeSceneReceived(string peerId, List scenes, SceneConfig currentScene) { onChangeScene?.Invoke(peerId, scenes, currentScene); } public void CloseReceived(JsonData data) { OnClose?.Invoke(data); } }