WSHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;
  5. using System;
  6. using XRTool.Util;
  7. using ShadowStudio.UI;
  8. using ShadowStudio.Model;
  9. using UnityEngine.SceneManagement;
  10. using BeinLab.Util;
  11. using BestHTTP.WebSocket;
  12. using ShadowStudio.Tool;
  13. using Studio.WebSocket.Message;
  14. public class WSHandler
  15. {
  16. public static WebSocketClient _client;
  17. public static WebSocketClient _roomClient;
  18. public static WebSocketClient _roomRtcClient;
  19. private static WSUserHandler _user;
  20. private static WSRoomHandler _room;
  21. private static WSRtcHandler _Rtc;
  22. private static WSGlobalHandler _global;
  23. private static WSPaintingHandler _painting;
  24. private static WSRtcHandler _webRtc;
  25. public static WSRtcHandler Rtc
  26. {
  27. get
  28. {
  29. if (_Rtc == null)
  30. {
  31. _Rtc = new WSRtcHandler();
  32. }
  33. return _Rtc;
  34. }
  35. }
  36. public static WSUserHandler User
  37. {
  38. get
  39. {
  40. if (_user == null)
  41. {
  42. _user = new WSUserHandler();
  43. }
  44. return _user;
  45. }
  46. }
  47. public static WSRoomHandler Room
  48. {
  49. get
  50. {
  51. if (_room == null)
  52. {
  53. _room = new WSRoomHandler();
  54. }
  55. return _room;
  56. }
  57. }
  58. public static WSRtcHandler WebRtc
  59. {
  60. get
  61. {
  62. if (_webRtc == null)
  63. {
  64. _webRtc = new WSRtcHandler();
  65. }
  66. return _webRtc;
  67. }
  68. }
  69. public static void init()
  70. {
  71. GameObject prefab = (GameObject)Resources.Load("websocket");
  72. GameObject websocket = GameObject.Instantiate(prefab);
  73. _client = websocket.GetComponent<WebSocketClient>();
  74. _client.OnMessaged += OnMessageReceived;
  75. _client.OnWebErrorClose += OnWebErrorClosed;
  76. _client.Connect(SpaceHttpAction.Instance.lobbyUrl,"大厅");
  77. _client._webSocket.OnError += OnError;
  78. }
  79. private static void OnError(WebSocket ws, Exception ex)
  80. {
  81. clientClosed();
  82. Debug.Log("OnErrorOnErrorOnErrorOnErrorOnErrorOnError"+ ex.Message);
  83. CommonMethod.ShowTextTusi();
  84. if (TuSi.Instance)
  85. {
  86. TuSi.Instance.ShowTextContent("大厅服务器网络不佳,正在重连。");
  87. //LanguageMgr.Instance.GetMessage("1056").Message
  88. }
  89. // CommonMethod.ShowPopUp(PopType.Normal, LanguageMgr.Instance.GetMessage("1059").Message, LanguageMgr.Instance.GetMessage("1104").Message, () =>
  90. // { LoadLoginSence(); }, LanguageMgr.Instance.GetMessage("1066").Message);
  91. TimerMgr.Instance.CreateTimer(()=> { init(); },1f);
  92. }
  93. private static void OnWebErrorClosed(string data)
  94. {
  95. clientClosed();
  96. CommonMethod.ShowTextTusi();
  97. if (TuSi.Instance)
  98. {
  99. TuSi.Instance.ShowTextContent("大厅服务器网络不佳,正在重连。");
  100. //LanguageMgr.Instance.GetMessage("1056").Message
  101. }
  102. // CommonMethod.ShowPopUp(PopType.Normal, LanguageMgr.Instance.GetMessage("1059").Message, LanguageMgr.Instance.GetMessage("1104").Message, () =>
  103. // { LoadLoginSence(); }, LanguageMgr.Instance.GetMessage("1066").Message);
  104. TimerMgr.Instance.CreateTimer(() => { init(); }, 1f);
  105. }
  106. public static void clientClosed()
  107. {
  108. if (_client)
  109. {
  110. _client.CloseWebSocket();
  111. GameObject.Destroy(_client.gameObject);
  112. }
  113. }
  114. public static string _roomid;
  115. public static void roominit(string roomid)
  116. {
  117. _roomid = roomid;
  118. GameObject prefab = (GameObject)Resources.Load("websocket");
  119. GameObject websocket = GameObject.Instantiate(prefab);
  120. _roomClient = websocket.GetComponent<WebSocketClient>();
  121. _roomClient.OnWebErrorClose += OnRoomWebErrorClosed;
  122. _roomClient.OnMessaged += OnRoomMessageReceived;
  123. _roomClient.Connect(SpaceHttpAction.Instance.roomUrl + UserInfo.User_Token + "&roomId=" + roomid,"房间");
  124. }
  125. public static void roomRtcinit()
  126. {
  127. // AgoraVideoAudioManager.Instance.JoinChannel();
  128. Debug.Log("rtc==============================>");
  129. GameObject prefab = (GameObject)Resources.Load("websocket");
  130. GameObject websocket = GameObject.Instantiate(prefab);
  131. _roomRtcClient = websocket.GetComponent<WebSocketClient>();
  132. _roomRtcClient.OnWebErrorClose += OnRoomRtcWebErrorClosed;
  133. _roomRtcClient.OnMessaged += OnRoomRtcMessageReceived;
  134. //
  135. // // _roomRtcClient.Connect("wss://apitest.shadowcreator.com:6443/?roomId=" + _roomid);
  136. // // _roomRtcClient.Connect("wss://cgdemo.shadowcreator.com/rtc/?roomId=" + _roomid, "音视频");
  137. // _roomRtcClient.Connect(BuildConfig.Instance.rtcUrl+"?roomId=" + _roomid,"音视频");
  138. //// _roomRtcClient.Connect("wss://api.shadowcreator.com:443/rtcwss/?roomId=" + _roomid);
  139. // _roomRtcClient._webSocket.OnOpen += OnrtcOpen;
  140. // _roomRtcClient._webSocket.OnMessage += OnRTCMessage;
  141. }
  142. private static void OnRTCMessage(WebSocket webSocket, string message)
  143. {
  144. if (SCRtcFactory.Instance != null)
  145. {
  146. JsonData data = JsonMapper.ToObject(message);
  147. if (data.Keys.Contains("id") && data["id"].ToString() == WSRtcHandler.produceid)
  148. {
  149. SCRtcFactory.Instance.mSCRtcHandle.setProduce(data["data"]["id"].ToString());
  150. }
  151. else if(data.Keys.Contains("request") && data.Keys.Contains("method") && data["method"].ToString()== "newConsumer")
  152. {
  153. }
  154. else if (data.Keys.Contains("request"))
  155. {
  156. Rtc.RtcResponse(data);
  157. }
  158. }
  159. }
  160. public static void OnrtcOpen(WebSocket ws)
  161. {
  162. // StudioRtc.Instance.start(_roomid);
  163. }
  164. public static void OnRoomRtcMessageReceived(string message)
  165. {
  166. UnityLog.Instance.Log("SocketRoomRtcMessage===>" + message, 1);
  167. // SCRtcFactory.Instance.mSCRtcHandle.onSocketMesseage(message);
  168. message_data.Clear();
  169. message_data = JsonMapper.ToObject(message);
  170. Rtc.OnMessageReceived(message_data);
  171. // message_data.Clear();
  172. // message_data = JsonMapper.ToObject(message);
  173. // Room.OnMessageReceived(message_data, message);
  174. }
  175. public static void roomRtcCloes()
  176. {
  177. try
  178. {
  179. //if (StudioRtc.Instance)
  180. //{
  181. // StudioRtc.Instance.Close();
  182. //}
  183. //if (_roomRtcClient)
  184. //{
  185. // _roomRtcClient.CloseWebSocket();
  186. // GameObject.Destroy(_roomRtcClient.gameObject);
  187. //}
  188. }
  189. catch
  190. {
  191. // UnityLog.Instance.LogError("退出房间异常"+ _roomRtcClient);
  192. }
  193. }
  194. public static void OnRoomRtcWebErrorClosed(string message)
  195. {
  196. roomCloes();
  197. Debug.Log("OnRoomRtcWebErrorClosedOnRoomRtcWebErrorClosedOnRoomRtcWebErrorClosedOnRoomRtcWebErrorClosedOnRoomRtcWebErrorClosed");
  198. CommonMethod.ShowPopUp(PopType.Normal, LanguageMgr.Instance.GetMessage("1059").Message, LanguageMgr.Instance.GetMessage("1104").Message, () =>
  199. { LoadLoginSence(); }, LanguageMgr.Instance.GetMessage("1066").Message);
  200. }
  201. public static void LoadLoginSence()
  202. {
  203. roomCloes();
  204. clientClosed();
  205. if (BoardDlg.Instance != null)
  206. {
  207. BoardDlg.Instance.DestoryARcamera();
  208. }
  209. SceneManager.LoadScene("Login");
  210. }
  211. public static void roomCloes()
  212. {
  213. if(_roomClient)
  214. {
  215. _roomClient.CloseWebSocket();
  216. GameObject.Destroy(_roomClient.gameObject);
  217. }
  218. // roomRtcCloes();
  219. }
  220. public static void OnRoomWebErrorClosed(string message)
  221. {
  222. roomCloes();
  223. Debug.Log("OnRoomWebErrorClosedOnRoomWebErrorClosedOnRoomWebErrorClosedOnRoomWebErrorClosedOnRoomWebErrorClosedOnRoomWebErrorClosed"+ message);
  224. CommonMethod.ShowTextTusi();
  225. if (TuSi.Instance)
  226. {
  227. TuSi.Instance.ShowTextContent("房间服务器网络不佳,正在重连。");
  228. //LanguageMgr.Instance.GetMessage("1056").Message
  229. }
  230. TimerMgr.Instance.CreateTimer(() =>
  231. {
  232. roominit(_roomid);
  233. WSHandler.Room.OnInit -= onRoomInit;
  234. WSHandler.Room.OnInit += onRoomInit;
  235. }, 1f);
  236. }
  237. private static void onRoomInit(JsonData data)
  238. {
  239. WSHandler.Room.OnJoinRoom -= onJoinRoom;
  240. WSHandler.Room.OnJoinRoom += onJoinRoom;
  241. WSHandler.Room.JoinRoom(CommonMethod.roomConfig.Id, CommonMethod.roomConfig.Room_password);
  242. // roomRtcinit();
  243. }
  244. private static void onJoinRoom(List<GoodsInfo> goods, List<Peer> peers, List<SceneConfig> scenes, SceneConfig currentScene)
  245. {
  246. CommonMethod.IsReceive = false;
  247. }
  248. public static void OnRoomMessageReceived(string message)
  249. {
  250. // UnityLog.Instance.Log("SocketRoomMessage===>" + message, 4);
  251. message_data.Clear();
  252. message_data = JsonMapper.ToObject(message);
  253. Room.OnMessageReceived(message_data, message);
  254. }
  255. public static void SendRoomMessage(JsonData data)
  256. {
  257. SendRoomMessage(data.ToJson());
  258. }
  259. public static void SendRoomMessage(string data)
  260. {
  261. Debug.Log("SendRoomMessage===>" + data);
  262. // UnityLog.Instance.Log("SendRoomMessage===>" + data, 4);
  263. if (_roomClient != null)
  264. _roomClient.Send(data);
  265. }
  266. public static void SendRtcMessage(JsonData data)
  267. {
  268. UnityLog.Instance.Log("SendRtcMessage===>" + data.ToJson(), 1);
  269. string message = data.ToJson();
  270. if (_roomRtcClient != null)
  271. _roomRtcClient.Send(message);
  272. }
  273. public static void SendMessage(JsonData data)
  274. {
  275. UnityLog.Instance.Log("SendMessage===>" + data.ToJson(), 4);
  276. string message = data.ToJson();
  277. if (_client != null)
  278. _client.Send(message);
  279. }
  280. private static JsonData message_data = new JsonData();
  281. public static void OnMessageReceived(string message)
  282. {
  283. // Debug.Log("SocketMessage===>" + message);
  284. UnityLog.Instance.Log("SocketMessage===>" + message, 4);
  285. //message_data.Clear();
  286. //message_data = JsonMapper.ToObject(message);
  287. User.OnMessageReceived(message);
  288. }
  289. }