123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- 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<int> 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<int> 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<string, string> idList = new Dictionary<string, string>();
- 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);
- }
- /// <summary>
- /// 创建场景
- /// </summary>
- public void CreateScene()
- {
- SendRoomData("createScene", datas);
- }
- /// <summary>
- /// 切换场景
- /// </summary>
- /// <param name="sceneId"></param>
- public void ChangeScene(int sceneId)
- {
- SendRoomData("changeScene", Returndats(sceneId));
- }
- /// <summary>
- /// 删除场景
- /// </summary>
- /// <param name="sceneId"></param>
- public void DeleteScene(int sceneId)
- {
- SendRoomData("deleteScene", Returndats(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)
- {
- 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<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);
-
- 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<int> scenes, int currentScene)
- {
- onChangeScene?.Invoke(peerId, scenes, currentScene);
- }
- public void CloseReceived(JsonData data)
- {
- OnClose?.Invoke(data);
- }
- }
|