WSRoomHandler.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using LitJson;
  2. using Newtonsoft.Json;
  3. using ShadowStudio.Model;
  4. using ShadowStudio.UI;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using XRTool.Util;
  9. public class BroadcastGoods//广播进行同步
  10. {
  11. public bool request;
  12. public int id;
  13. public string method;
  14. }
  15. public class BroadcastGood//创建物体
  16. {
  17. public bool request;
  18. public int id;
  19. public string method;
  20. }
  21. public class JoinReturnGoods
  22. {
  23. public bool request;
  24. public int id;
  25. public bool ok;
  26. public JoinData data;
  27. }
  28. public class JoinData
  29. {
  30. public List<int> scenes;
  31. public int currentScene;
  32. public string peerId;
  33. }
  34. public struct SendData
  35. {
  36. public bool request;
  37. public int id;
  38. public string method;
  39. public Datas data;
  40. }
  41. public struct Datas
  42. {
  43. public int sceneId;
  44. public string goodsId;
  45. public string action;
  46. public string requesttime;
  47. public string sign;
  48. public string user_token;
  49. public string room_id;
  50. }
  51. public class WSRoomHandler
  52. {
  53. public BroadcastGoods broadcastGoods = new BroadcastGoods();
  54. public JoinReturnGoods joinReturnGoods = new JoinReturnGoods();
  55. public delegate void OnInitDelegate(JsonData data);
  56. public delegate void OnOtherUserJoinRoomDelegate(JsonData data);
  57. public delegate void OnOtherUserLeaveRoomDelegate(JsonData data);
  58. public delegate void OnMessageForwardDelegate(JsonData data);
  59. public delegate void OnListenCreateGoodDelegate(JsonData data);
  60. public delegate void OnListenDeleteGoodDelegate(JsonData data);
  61. public delegate void OnListenBroadcastGoodDelegate(JsonData data);
  62. public delegate void OnListenMessageForwardDelegate(JsonData data);
  63. public delegate void OnChangeSceneDelegate(string peerId, List<int> scenes, int currentScene);
  64. public delegate void OnCloseDelegate(JsonData data);
  65. public OnInitDelegate OnInit;
  66. public OnOtherUserJoinRoomDelegate OnOtherUserJoinRoom;
  67. public OnOtherUserLeaveRoomDelegate OnOtherUserLeaveRoom;
  68. public OnMessageForwardDelegate OnMessageForward;
  69. public OnListenCreateGoodDelegate OnListenCreateGood;
  70. public OnListenDeleteGoodDelegate OnListenDeleteGood;
  71. public OnListenBroadcastGoodDelegate OnListenBroadcastGood;
  72. public OnListenMessageForwardDelegate OnListenMessageForward;
  73. public OnChangeSceneDelegate onChangeScene;
  74. public OnCloseDelegate OnClose;
  75. private Dictionary<string, string> idList = new Dictionary<string, string>();
  76. private SendData sendData = new SendData();
  77. private Datas datas = new Datas();
  78. JsonData SendData = new JsonData();
  79. public void SendRoomData(string method, Datas data)
  80. {
  81. sendData.request = true;
  82. sendData.id = Random.Range(10000000, 99999999);
  83. sendData.method = method;
  84. sendData.data = data;
  85. string message = JsonConvert.SerializeObject(sendData);
  86. CommonMethod.ShowNetLoading(WSHandler._roomClient.name,"房间通讯中...");
  87. WSHandler.SendRoomMessage(message);
  88. }
  89. public Datas Returndats(int sceneId, string goodsId="")
  90. {
  91. datas.sceneId = sceneId;
  92. datas.goodsId = goodsId;
  93. return datas;
  94. }
  95. public void JoinRoom(string id, string pwd)
  96. {
  97. JsonData message = new JsonData();
  98. JsonData data = new JsonData();
  99. message["request"] = true;
  100. message["id"] = Random.Range(10000000, 99999999);
  101. message["method"] = "joinRoom";
  102. data["roomId"] = id;
  103. data["roomPwd"] = pwd;
  104. message["data"] = data;
  105. idList.Add(message["id"].ToString(), message["method"].ToString());
  106. CommonMethod.ShowNetLoading(WSHandler._roomClient.name,"加入房间中...");
  107. WSHandler.SendRoomMessage(message);
  108. }
  109. public void ExitRoom()
  110. {
  111. JsonData message = new JsonData();
  112. JsonData data = new JsonData();
  113. message["request"] = true;
  114. message["id"] = Random.Range(10000000, 99999999);
  115. message["method"] = "leaveRoom";
  116. message["data"] = data;
  117. CommonMethod.ShowNetLoading(WSHandler._roomClient.name,"退出房间中...");
  118. WSHandler.SendRoomMessage(message);
  119. }
  120. /// <summary>
  121. /// 创建场景
  122. /// </summary>
  123. public void CreateScene()
  124. {
  125. SendRoomData("createScene", datas);
  126. }
  127. /// <summary>
  128. /// 切换场景
  129. /// </summary>
  130. /// <param name="sceneId"></param>
  131. public void ChangeScene(int sceneId)
  132. {
  133. SendRoomData("changeScene", Returndats(sceneId));
  134. }
  135. /// <summary>
  136. /// 删除场景
  137. /// </summary>
  138. /// <param name="sceneId"></param>
  139. public void DeleteScene(int sceneId)
  140. {
  141. SendRoomData("deleteScene", Returndats(sceneId));
  142. }
  143. /// <summary>
  144. /// 物体场景切换
  145. /// </summary>
  146. /// <param name="sceneId"></param>
  147. /// <param name="goodsId"></param>
  148. public void GoodsModiScene(int sceneId,string goodsId)
  149. {
  150. SendRoomData("goodsModiScene", Returndats(sceneId, goodsId));
  151. }
  152. /// <summary>
  153. /// 物体存档
  154. /// </summary>
  155. /// <param name="sceneId"></param>
  156. /// <param name="goodsId"></param>
  157. public void Archiving(int sceneId, string goodsId)
  158. {
  159. SendRoomData("archiving", datas);
  160. }
  161. public void OnMessageReceived(JsonData data, string message)
  162. {
  163. if (data.Keys.Contains("response"))
  164. {
  165. CommonMethod.HideNetLoading(WSHandler._roomClient.name);
  166. }
  167. if (data.Keys.Contains("errorCode"))
  168. {
  169. CommonMethod.HideLoading();
  170. CommonMethod.ShowTextTusi();
  171. if (data.Keys.Contains("errorReason"))
  172. {
  173. }
  174. else
  175. {
  176. }
  177. return;
  178. }
  179. if (data.Keys.Contains("method"))
  180. {
  181. switch (data["method"].ToString())
  182. {
  183. case "connect":
  184. InitReceived(data);
  185. WSHandler.roomRtcinit(WSHandler._roomid);
  186. break;
  187. case "joinRoom"://其他用户加入房间
  188. OtherUserJoinRoomReceived(data);
  189. break;
  190. case "leaveRoom"://其他用户离开房间
  191. OtherUserLeaveRoomReceived(data);
  192. break;
  193. case "close"://账号在其他地方登陆
  194. CloseReceived(data);
  195. break;
  196. case "changeScene":
  197. JoinReturnGoods returnGoods = JsonConvert.DeserializeObject<JoinReturnGoods>(message);
  198. if (returnGoods != null)
  199. {
  200. ChangeSceneReceived(returnGoods.data.peerId, returnGoods.data.scenes, returnGoods.data.currentScene);
  201. }
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. if (data.Keys.Contains("id"))
  208. {
  209. if (idList.ContainsKey(data["id"].ToString()))
  210. {
  211. switch (idList[data["id"].ToString()])
  212. {
  213. case "joinRoom"://获取已经在房间的用户信息
  214. JoinReturnGoods returnGoods = JsonConvert.DeserializeObject<JoinReturnGoods>(message);
  215. break;
  216. default:
  217. break;
  218. }
  219. idList.Remove(data["id"].ToString());
  220. }
  221. }
  222. }
  223. private void InitReceived(JsonData data)
  224. {
  225. if (OnInit != null)
  226. {
  227. OnInit(data);
  228. }
  229. }
  230. private void OtherUserJoinRoomReceived(JsonData data)
  231. {
  232. if (OnOtherUserJoinRoom != null)
  233. {
  234. OnOtherUserJoinRoom(data);
  235. }
  236. }
  237. private void OtherUserLeaveRoomReceived(JsonData data)
  238. {
  239. if (OnOtherUserLeaveRoom != null)
  240. {
  241. OnOtherUserLeaveRoom(data);
  242. }
  243. }
  244. public void ChangeSceneReceived(string peerId, List<int> scenes, int currentScene)
  245. {
  246. onChangeScene?.Invoke(peerId, scenes, currentScene);
  247. }
  248. public void CloseReceived(JsonData data)
  249. {
  250. OnClose?.Invoke(data);
  251. }
  252. }