WSRoomHandler.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. using LitJson;
  2. using Newtonsoft.Json;
  3. using ShadowStudio.Model;
  4. using ShadowStudio.UI;
  5. using Studio.WebSocket.Message;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using XRTool.Util;
  11. using Random = UnityEngine.Random;
  12. public class NotifyGoods
  13. {
  14. public bool notification = true;
  15. public string method = "transmitGoods";
  16. public List<GoodsInfo> data;
  17. }
  18. public class BroadcastGoods//广播进行同步
  19. {
  20. public bool request;
  21. public int id;
  22. public string method;
  23. public List<GoodsInfo> data;
  24. }
  25. public class BroadcastGood//创建物体
  26. {
  27. public bool request;
  28. public int id;
  29. public string method;
  30. public GoodsInfo data;
  31. }
  32. public class JoinReturnGoods
  33. {
  34. public bool request;
  35. public int id;
  36. public bool ok;
  37. public JoinData data;
  38. }
  39. public class JoinData
  40. {
  41. public List<GoodsInfo> goods;
  42. public List<Peer> peers;
  43. public List<SceneConfig> scenes;
  44. public SceneConfig currentScene;
  45. public string peerId;
  46. }
  47. public struct SendData
  48. {
  49. public bool request;
  50. public int id;
  51. public string method;
  52. public SceneDatas data;
  53. }
  54. public struct Datas
  55. {
  56. public int sceneId;
  57. public string goodsId;
  58. public string action;
  59. public string requesttime;
  60. public string sign;
  61. public string user_token;
  62. public string room_id;
  63. public GoodsInfo good;
  64. }
  65. public struct SceneDatas
  66. {
  67. public int roomID;
  68. public string sceneName;
  69. public int sceneId;
  70. }
  71. public class WSRoomHandler
  72. {
  73. public BroadcastGoods broadcastGoods = new BroadcastGoods();
  74. public NotifyGoods notifyGoods = new NotifyGoods();
  75. public JoinReturnGoods joinReturnGoods = new JoinReturnGoods();
  76. public delegate void OnInitDelegate(JsonData data);
  77. public delegate void OnJoinRoomDelegate(List<GoodsInfo> goods, List<Peer> peers, List<SceneConfig> scenes, SceneConfig currentScene);
  78. public delegate void OnOtherUserJoinRoomDelegate(JsonData data);
  79. public delegate void OnOtherUserLeaveRoomDelegate(JsonData data);
  80. public delegate void OnBroadcastGoodDelegate(List<GoodsInfo> data);
  81. public delegate void OnMessageForwardDelegate(JsonData data);
  82. public delegate void OnListenCreateGoodDelegate(JsonData data);
  83. public delegate void OnListenDeleteGoodDelegate(JsonData data);
  84. public delegate void OnListenBroadcastGoodDelegate(JsonData data);
  85. public delegate void OnListenMessageForwardDelegate(JsonData data);
  86. public delegate void OnChangeSceneDelegate(string peerId, List<SceneConfig> scenes, SceneConfig currentScene);
  87. public delegate void OnCloseDelegate(JsonData data);
  88. public OnInitDelegate OnInit;
  89. public OnJoinRoomDelegate OnJoinRoom;
  90. public OnOtherUserJoinRoomDelegate OnOtherUserJoinRoom;
  91. public OnOtherUserLeaveRoomDelegate OnOtherUserLeaveRoom;
  92. public OnBroadcastGoodDelegate OnBroadcastGood;
  93. public OnMessageForwardDelegate OnMessageForward;
  94. public OnListenCreateGoodDelegate OnListenCreateGood;
  95. public OnListenDeleteGoodDelegate OnListenDeleteGood;
  96. public OnListenBroadcastGoodDelegate OnListenBroadcastGood;
  97. public OnListenMessageForwardDelegate OnListenMessageForward;
  98. public OnChangeSceneDelegate onChangeScene;
  99. public OnCloseDelegate OnClose;
  100. private Dictionary<string, string> idList = new Dictionary<string, string>();
  101. private SendData sendData = new SendData();
  102. private Datas datas = new Datas();
  103. private SceneDatas sceneDatas = new SceneDatas();
  104. JsonData SendData = new JsonData();
  105. Dictionary<int, Action<JsonData>> dicRequest = new Dictionary<int, Action<JsonData>>();
  106. public void SendRoomData(string method, SceneDatas data, Action<JsonData> callBack=null)
  107. {
  108. sendData.request = true;
  109. sendData.id = Random.Range(10000000, 99999999);
  110. sendData.method = method;
  111. sendData.data = data;
  112. string message = JsonConvert.SerializeObject(sendData);
  113. if (WSHandler._roomClient)
  114. {
  115. CommonMethod.ShowNetLoading(WSHandler._roomClient.name, "房间通讯中...");
  116. }
  117. else
  118. {
  119. UnityLog.Instance.LogError("SendRoomData _roomClient is destroy!");
  120. }
  121. if(callBack!=null)
  122. {
  123. dicRequest.Add(sendData.id, callBack);
  124. }
  125. WSHandler.SendRoomMessage(message);
  126. }
  127. public SceneDatas ReturnScennedats(int sceneId, string sceneName = "")
  128. {
  129. sceneDatas.roomID = int.Parse(ShowViewMgr.Instance.roomId);
  130. sceneDatas.sceneId = sceneId;
  131. sceneDatas.sceneName = sceneName;
  132. return sceneDatas;
  133. }
  134. public SceneDatas ReturnScennedats(string sceneName)
  135. {
  136. sceneDatas.roomID = int.Parse(ShowViewMgr.Instance.roomId);
  137. sceneDatas.sceneName = sceneName;
  138. return sceneDatas;
  139. }
  140. public Datas Returndats(GoodsInfo good)
  141. {
  142. datas.action = "20300";
  143. datas.room_id = ShowViewMgr.Instance.roomId;
  144. datas.requesttime = CommonMethod.GetCurrentTimeUnix().ToString();
  145. datas.user_token = UserInfo.User_Token;
  146. datas.good = good;
  147. datas.sign = "588cd263bfd7a7b5e561dba46b989ef1";
  148. return datas;
  149. }
  150. public void JoinRoom(string id, string pwd)
  151. {
  152. JsonData message = new JsonData();
  153. JsonData data = new JsonData();
  154. message["request"] = true;
  155. message["id"] = Random.Range(10000000, 99999999);
  156. message["method"] = "joinRoom";
  157. data["roomId"] = id;
  158. data["roomPwd"] = pwd;
  159. message["data"] = data;
  160. idList.Add(message["id"].ToString(), message["method"].ToString());
  161. CommonMethod.ShowNetLoading(WSHandler._roomClient.name, "加入房间中...");
  162. WSHandler.SendRoomMessage(message);
  163. // AgoraVideoAudioManager.Instance.JoinChannel(id);
  164. }
  165. public void ExitRoom()
  166. {
  167. JsonData message = new JsonData();
  168. JsonData data = new JsonData();
  169. message["request"] = true;
  170. message["id"] = Random.Range(10000000, 99999999);
  171. message["method"] = "leaveRoom";
  172. message["data"] = data;
  173. CommonMethod.ShowNetLoading(WSHandler._roomClient.name, "退出房间中...");
  174. WSHandler.SendRoomMessage(message);
  175. }
  176. public void CreateGood(int aid,string art_id, string goods_name, string goods_info, string goods_extended, int status = 0)
  177. {
  178. string roomid = "";
  179. if (ShowViewMgr.Instance != null)
  180. {
  181. roomid = ShowViewMgr.Instance.roomId;
  182. }
  183. CreateGood(aid,art_id, goods_name, goods_info, goods_extended, roomid, status);
  184. }
  185. public void CreateGood(int aid,string art_id, string goods_name, string goods_info, string goods_extended, string room_id, int status = 0)
  186. {
  187. if (WSHandler._roomClient)
  188. {
  189. JsonData message = new JsonData();
  190. JsonData data = new JsonData();
  191. message["request"] = true;
  192. message["id"] = Random.Range(10000000, 99999999);
  193. message["method"] = "createGoods";
  194. data["aid"] = aid;
  195. data["action"] = "20300";
  196. data["art_id"] = art_id;
  197. data["goods_name"] = goods_name;
  198. data["goods_info"] = goods_info;
  199. data["goods_extended"] = goods_extended;
  200. data["room_id"] = room_id;
  201. data["status"] = status;
  202. data["scene_id"] = CommonMethod.currentScene.Id;
  203. data["requesttime"] = CommonMethod.GetCurrentTimeUnix().ToString();
  204. data["user_token"] = UserInfo.User_Token;
  205. data["sign"] = "588cd263bfd7a7b5e561dba46b989ef1";
  206. message["data"] = data;
  207. CommonMethod.ShowNetLoading(WSHandler._roomClient.name, "创建物体...");
  208. WSHandler.SendRoomMessage(message);
  209. }
  210. }
  211. public void CreateGood(GoodsInfo goods)
  212. {
  213. if (ShowViewMgr.Instance)
  214. {
  215. //SendRoomData("createGoods", Returndats(goods));//这个是等服务器优化过数据结构后要用的
  216. CreateGood(goods.rid,goods.art_id, goods.goods_name, goods.goods_info, goods.goods_extended, ShowViewMgr.Instance.roomId, goods.status);
  217. }
  218. }
  219. public void DeleteGood(int[] goods_id)
  220. {
  221. JsonData message = new JsonData();
  222. JsonData data = new JsonData();
  223. message["request"] = true;
  224. message["id"] = Random.Range(10000000, 99999999);
  225. message["method"] = "deleteGoods";
  226. data["action"] = "20303";
  227. ///曹霆增加 2021/6/10 服务器要求增加(李圣辉)
  228. data["sceneID"] = CommonMethod.currentScene.Id;
  229. data["goods_ids"] = JsonMapper.ToObject(JsonConvert.SerializeObject(goods_id));
  230. //data["goods_ids"].Add (goods_id);
  231. data["requesttime"] = CommonMethod.GetCurrentTimeUnix().ToString();
  232. data["user_token"] = UserInfo.User_Token;
  233. data["sign"] = "588cd263bfd7a7b5e561dba46b989ef1";
  234. message["data"] = data;
  235. CommonMethod.ShowNetLoading(WSHandler._roomClient.name, "删除物体...");
  236. WSHandler.SendRoomMessage(message);
  237. }
  238. public void BroadcastGood(List<GoodsInfo> goodsInfos, string method = "transmitGoods")
  239. {
  240. broadcastGoods.request = true;
  241. broadcastGoods.id = Random.Range(10000000, 99999999);
  242. broadcastGoods.method = method;
  243. broadcastGoods.data = goodsInfos;
  244. // CommonMethod.ShowNetLoading(WSHandler._roomClient.name);
  245. WSHandler.SendRoomMessage(JsonConvert.SerializeObject(broadcastGoods));
  246. }
  247. public void BroadNotifycastGood(List<GoodsInfo> goodsInfos, string method = "transmitGoods")
  248. {
  249. notifyGoods.data = goodsInfos;
  250. notifyGoods.method = method;
  251. WSHandler.SendRoomMessage(JsonConvert.SerializeObject(notifyGoods));
  252. }
  253. /// <summary>
  254. /// 转发信息
  255. /// </summary>
  256. /// <param name="goodsInfos"></param>
  257. public void TransmitGood(List<GoodsInfo> goodsInfos)
  258. {
  259. BroadNotifycastGood(goodsInfos, "transmitGoods");
  260. //BroadcastGood(goodsInfos, "transmit");
  261. }
  262. /// <summary>
  263. /// 创建场景
  264. /// </summary>
  265. public void CreateScene(string sceneName)
  266. {
  267. SendRoomData("createScene", ReturnScennedats(sceneName));
  268. }
  269. /// <summary>
  270. /// 切换场景
  271. /// </summary>
  272. /// <param name="sceneId"></param>
  273. public void ChangeScene(int sceneId)
  274. {
  275. SendRoomData("changeScene", ReturnScennedats(sceneId));
  276. }
  277. /// <summary>
  278. /// 场景名字
  279. /// </summary>
  280. /// <param name="sceneId"></param>
  281. public void ChangeSceneName(int sceneId, string sceneName)
  282. {
  283. SendRoomData("updateScene", ReturnScennedats(sceneId, sceneName));
  284. }
  285. /// <summary>
  286. /// 删除场景
  287. /// </summary>
  288. /// <param name="sceneId"></param>
  289. public void DeleteScene(int sceneId)
  290. {
  291. SendRoomData("deleteScene", ReturnScennedats(sceneId));
  292. }
  293. /// <summary>
  294. /// 物体场景切换
  295. /// </summary>
  296. /// <param name="sceneId"></param>
  297. /// <param name="goodsId"></param>
  298. public void GoodsModiScene(int sceneId, string goodsId)
  299. {
  300. //SendRoomData("goodsModiScene", Returndats(sceneId, goodsId));
  301. }
  302. /// <summary>
  303. /// 物体存档
  304. /// </summary>
  305. /// <param name="sceneId"></param>
  306. /// <param name="goodsId"></param>
  307. public void Archiving(int sceneId, string goodsId)
  308. {
  309. //SendRoomData("archiving", datas);
  310. }
  311. public void OnMessageReceived(JsonData data, string message)
  312. {
  313. //Debug.Log("data====>"+ data.ToJson());
  314. if (data.Keys.Contains("response"))
  315. {
  316. CommonMethod.HideNetLoading(WSHandler._roomClient.name);
  317. if(dicRequest.ContainsKey(int.Parse(data["id"].ToString())))
  318. {
  319. dicRequest[int.Parse(data["id"].ToString())].Invoke(data);
  320. dicRequest.Remove(int.Parse(data["id"].ToString()));
  321. }
  322. if(message.Contains("uid"))
  323. {
  324. Debug.Log(data["data"]["peers"][0]["peerId"].ToString());
  325. Debug.Log((uint)Int32.Parse(data["data"]["peers"][0]["uid"].ToString()));
  326. AgoraRTCManager.Instance.AddDicPeerData(data["data"]["peers"][0]["peerId"].ToString(), (uint)Int32.Parse(data["data"]["peers"][0]["uid"].ToString()));
  327. }
  328. }
  329. if (data.Keys.Contains("errorCode"))
  330. {
  331. CommonMethod.HideLoading();
  332. CommonMethod.ShowTextTusi();
  333. if (TuSi.Instance)
  334. {
  335. if (data.Keys.Contains("errorReason"))
  336. {
  337. TuSi.Instance.ShowTextContent(data["errorReason"].ToString());
  338. }
  339. else
  340. {
  341. TuSi.Instance.ShowTextContent(data["errorCode"]["message"].ToString());
  342. string code = data["errorCode"]["code"].ToString();
  343. if (code == "5089"|| code == "5091" || code == "5093" || code == "5094")
  344. {
  345. WSHandler.roomCloes();
  346. }
  347. }
  348. }
  349. //被zby替换的代码
  350. //if (RoomListDlg.Instance && RoomListDlg.Instance.roomlist)
  351. //{
  352. // RoomListDlg.Instance.roomlist.RefeshisClick();
  353. //}
  354. if (MenuListDlg.Instance && MenuListDlg.Instance._menuUI)
  355. {
  356. //当进入房间失败时,取消事件监听
  357. MenuListDlg.Instance._menuUI.RefeshisClick();
  358. }
  359. return;
  360. }
  361. if (data.Keys.Contains("method"))
  362. {
  363. switch (data["method"].ToString())
  364. {
  365. case "transmitGoods":
  366. BroadcastGoods broadcast = JsonConvert.DeserializeObject<BroadcastGoods>(message);
  367. if (broadcast != null)
  368. {
  369. BroadcastGoodReceived(broadcast.data);
  370. }
  371. break;
  372. case "transmit":
  373. BroadcastGoods broad = JsonConvert.DeserializeObject<BroadcastGoods>(message);
  374. if (broad != null)
  375. {
  376. BroadcastGoodReceived(broad.data);
  377. }
  378. break;
  379. case "connect":
  380. InitReceived(data);
  381. break;
  382. case "joinRoom"://其他用户加入房间
  383. OtherUserJoinRoomReceived(data);
  384. break;
  385. case "createGoods":
  386. CreateGoodReceived(message);
  387. break;
  388. case "deleteGoods":
  389. DeleteGoodReceived(data);
  390. break;
  391. case "leaveRoom"://其他用户离开房间
  392. OtherUserLeaveRoomReceived(data);
  393. break;
  394. case "close"://账号在其他地方登陆
  395. CloseReceived(data);
  396. break;
  397. case "changeScene":
  398. JoinReturnGoods returnGoods = JsonConvert.DeserializeObject<JoinReturnGoods>(message);
  399. if (returnGoods != null)
  400. {
  401. ChangeSceneReceived(returnGoods.data.peerId, returnGoods.data.scenes, returnGoods.data.currentScene);
  402. }
  403. break;
  404. default:
  405. break;
  406. }
  407. }
  408. if (data.Keys.Contains("id"))
  409. {
  410. if (idList.ContainsKey(data["id"].ToString()))
  411. {
  412. switch (idList[data["id"].ToString()])
  413. {
  414. case "joinRoom"://获取已经在房间的用户信息
  415. JoinReturnGoods returnGoods = JsonConvert.DeserializeObject<JoinReturnGoods>(message);
  416. if (returnGoods != null)
  417. {
  418. JoinRoomReceived(returnGoods.data.goods, returnGoods.data.peers, returnGoods.data.scenes, returnGoods.data.currentScene);
  419. }
  420. // WSHandler.roomRtcinit();
  421. // 进入房间
  422. break;
  423. default:
  424. break;
  425. }
  426. idList.Remove(data["id"].ToString());
  427. }
  428. }
  429. }
  430. public void InitReceived(JsonData data)
  431. {
  432. if (OnInit != null)
  433. {
  434. OnInit(data);
  435. Debug.Log(data["data"]["uid"]);
  436. Debug.Log(data["data"]["peerId"]);
  437. AgoraRTCManager.Instance.Uid = (uint)Int32.Parse(data["data"]["uid"].ToString());
  438. AgoraRTCManager.Instance.PeerId = data["data"]["peerId"].ToString();
  439. AgoraRTCManager.Instance.AddDicPeerData(data["data"]["peerId"].ToString(), (uint)Int32.Parse(data["data"]["uid"].ToString()));
  440. }
  441. }
  442. private void JoinRoomReceived(List<GoodsInfo> goods, List<Peer> peers, List<SceneConfig> scenes, SceneConfig currentScene)
  443. {
  444. if (OnJoinRoom != null)
  445. {
  446. OnJoinRoom(goods, peers, scenes, currentScene);
  447. }
  448. }
  449. private void OtherUserJoinRoomReceived(JsonData data)
  450. {
  451. if (OnOtherUserJoinRoom != null)
  452. {
  453. OnOtherUserJoinRoom(data);
  454. AgoraRTCManager.Instance.AddDicPeerData(data["data"]["peerId"].ToString(), (uint)Int32.Parse(data["data"]["uid"].ToString()));
  455. }
  456. }
  457. private void OtherUserLeaveRoomReceived(JsonData data)
  458. {
  459. if (OnOtherUserLeaveRoom != null)
  460. {
  461. OnOtherUserLeaveRoom(data);
  462. AgoraRTCManager.Instance.RemDicPeerData(data["data"]["peerId"].ToString());
  463. }
  464. }
  465. //接受服务器创建物体的消息
  466. private void CreateGoodReceived(string message)
  467. {
  468. BroadcastGood broadcast = JsonConvert.DeserializeObject<BroadcastGood>(message);
  469. if (broadcast != null)
  470. {
  471. ArtInfoMgr.Instance.SyncGoods(broadcast.data);
  472. }
  473. }
  474. private void DeleteGoodReceived(JsonData data)
  475. {
  476. List<int> list = JsonConvert.DeserializeObject<List<int>>(data["data"]["ids"].ToJson());
  477. if (list != null)
  478. {
  479. for (int i = 0; i < list.Count; i++)
  480. {
  481. ArtInfoMgr.Instance.GoodsDelSync(list[i]);
  482. }
  483. }
  484. }
  485. public void BroadcastGoodReceived(List<GoodsInfo> data)
  486. {
  487. if (OnBroadcastGood != null)
  488. {
  489. OnBroadcastGood(data);
  490. }
  491. }
  492. public void ChangeSceneReceived(string peerId, List<SceneConfig> scenes, SceneConfig currentScene)
  493. {
  494. onChangeScene?.Invoke(peerId, scenes, currentScene);
  495. }
  496. public void CloseReceived(JsonData data)
  497. {
  498. OnClose?.Invoke(data);
  499. }
  500. }