123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523 |
- 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<GoodsInfo> data;
- }
- public class BroadcastGoods//广播进行同步
- {
- public bool request;
- public int id;
- public string method;
- public List<GoodsInfo> 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<GoodsInfo> goods;
- public List<Peer> peers;
- public List<SceneConfig> 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<GoodsInfo> goods, List<Peer> peers, List<SceneConfig> scenes, SceneConfig currentScene);
- public delegate void OnOtherUserJoinRoomDelegate(JsonData data);
- public delegate void OnOtherUserLeaveRoomDelegate(JsonData data);
- public delegate void OnBroadcastGoodDelegate(List<GoodsInfo> 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<SceneConfig> 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<string, string> idList = new Dictionary<string, string>();
- private SendData sendData = new SendData();
- private Datas datas = new Datas();
- private SceneDatas sceneDatas = new SceneDatas();
- JsonData SendData = new JsonData();
- Dictionary<int, Action<JsonData>> dicRequest = new Dictionary<int, Action<JsonData>>();
- public void SendRoomData(string method, SceneDatas data, Action<JsonData> 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<GoodsInfo> 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<GoodsInfo> goodsInfos, string method = "transmitGoods")
- {
- notifyGoods.data = goodsInfos;
- notifyGoods.method = method;
- WSHandler.SendRoomMessage(JsonConvert.SerializeObject(notifyGoods));
- }
- /// <summary>
- /// 转发信息
- /// </summary>
- /// <param name="goodsInfos"></param>
- public void TransmitGood(List<GoodsInfo> goodsInfos)
- {
- BroadNotifycastGood(goodsInfos, "transmitGoods");
- //BroadcastGood(goodsInfos, "transmit");
- }
- /// <summary>
- /// 创建场景
- /// </summary>
- public void CreateScene(string sceneName)
- {
- SendRoomData("createScene", ReturnScennedats(sceneName));
- }
- /// <summary>
- /// 切换场景
- /// </summary>
- /// <param name="sceneId"></param>
- public void ChangeScene(int sceneId)
- {
- SendRoomData("changeScene", ReturnScennedats(sceneId));
- }
- /// <summary>
- /// 场景名字
- /// </summary>
- /// <param name="sceneId"></param>
- public void ChangeSceneName(int sceneId, string sceneName)
- {
- SendRoomData("updateScene", ReturnScennedats(sceneId, sceneName));
- }
- /// <summary>
- /// 删除场景
- /// </summary>
- /// <param name="sceneId"></param>
- public void DeleteScene(int sceneId)
- {
- SendRoomData("deleteScene", ReturnScennedats(sceneId));
- }
- /// <summary>
- /// 物体场景切换
- /// </summary>
- /// <param name="sceneId"></param>
- /// <param name="goodsId"></param>
- public void GoodsModiScene(int sceneId, string goodsId)
- {
- //SendRoomData("goodsModiScene", Returndats(sceneId, goodsId));
- }
- /// <summary>
- /// 物体存档
- /// </summary>
- /// <param name="sceneId"></param>
- /// <param name="goodsId"></param>
- 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<BroadcastGoods>(message);
- if (broadcast != null)
- {
- BroadcastGoodReceived(broadcast.data);
- }
- break;
- case "transmit":
- BroadcastGoods broad = JsonConvert.DeserializeObject<BroadcastGoods>(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<JoinReturnGoods>(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<JoinReturnGoods>(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<GoodsInfo> goods, List<Peer> peers, List<SceneConfig> 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<BroadcastGood>(message);
- if (broadcast != null)
- {
- ArtInfoMgr.Instance.SyncGoods(broadcast.data);
- }
- }
- private void DeleteGoodReceived(JsonData data)
- {
- List<int> list = JsonConvert.DeserializeObject<List<int>>(data["data"]["ids"].ToJson());
- if (list != null)
- {
- for (int i = 0; i < list.Count; i++)
- {
- ArtInfoMgr.Instance.GoodsDelSync(list[i]);
- }
- }
- }
- public void BroadcastGoodReceived(List<GoodsInfo> data)
- {
- if (OnBroadcastGood != null)
- {
- OnBroadcastGood(data);
- }
- }
- public void ChangeSceneReceived(string peerId, List<SceneConfig> scenes, SceneConfig currentScene)
- {
- onChangeScene?.Invoke(peerId, scenes, currentScene);
- }
- public void CloseReceived(JsonData data)
- {
- OnClose?.Invoke(data);
- }
- }
|