WSHandler.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 BestHTTP.WebSocket;
  11. using static ScenesManager;
  12. using static ShowLogin;
  13. public class WSHandler
  14. {
  15. public static WebSocketClient _client;
  16. public static WebSocketClient _roomClient;
  17. public static WebSocketClient _roomRtcClient;
  18. private static WSOfficeHandler _Office;
  19. private static WSRoomHandler _room;
  20. private static WSRtcHandler _Rtc;
  21. private static WSGlobalHandler _global;
  22. private static WSPaintingHandler _painting;
  23. private static WSRtcHandler _webRtc;
  24. public static WSRtcHandler Rtc
  25. {
  26. get
  27. {
  28. if (_Rtc == null)
  29. {
  30. _Rtc = new WSRtcHandler();
  31. }
  32. return _Rtc;
  33. }
  34. }
  35. public static WSOfficeHandler Office
  36. {
  37. get
  38. {
  39. if (_Office == null)
  40. {
  41. _Office = new WSOfficeHandler();
  42. }
  43. return _Office;
  44. }
  45. }
  46. public static WSRoomHandler Room
  47. {
  48. get
  49. {
  50. if (_room == null)
  51. {
  52. _room = new WSRoomHandler();
  53. }
  54. return _room;
  55. }
  56. }
  57. public static WSRtcHandler WebRtc
  58. {
  59. get
  60. {
  61. if (_webRtc == null)
  62. {
  63. _webRtc = new WSRtcHandler();
  64. }
  65. return _webRtc;
  66. }
  67. }
  68. public static void init()
  69. {
  70. GameObject prefab = (GameObject)Resources.Load("websocket");
  71. GameObject websocket = GameObject.Instantiate(prefab);
  72. _client = websocket.GetComponent<WebSocketClient>();
  73. _client.OnMessaged += OnMessageReceived;
  74. _client.OnWebErrorClose += OnWebErrorClosed;
  75. _client.Connect(NetWorkHeadersConfig.Remote_SocketHall_url+"token=" + UserInfo.User_Token+"&unionId="+UserInfo.UnionId, "大厅");
  76. _client._webSocket.OnError += OnError;
  77. }
  78. private static void OnError(WebSocket ws, Exception ex)
  79. {
  80. if (PopUpInfo.Instance)
  81. {
  82. PopUpInfo.Instance.ShowNetErrorPanel();
  83. }
  84. if (ListenDlg.Instance && ListenDlg.Instance.gameObject.activeSelf)
  85. {
  86. ListenDlg.Instance.ErrorToGuaduan();
  87. }
  88. WSHandler.roomRtcCloes();
  89. WSHandler.clientClosed();
  90. if (ShowRoom.Instance && ShowRoom.Instance.gameObject.activeSelf)
  91. {
  92. ShowRoom.Instance.NetErrorExitRoom();
  93. }
  94. TimerMgr.Instance.CreateTimer(() =>
  95. {
  96. if (RemoteRtc.Instance)
  97. {
  98. RemoteRtc.Instance.InitCenter();
  99. }
  100. }, 1f);
  101. }
  102. private static void OnWebErrorClosed(string data)
  103. {
  104. if (PopUpInfo.Instance)
  105. {
  106. PopUpInfo.Instance.ShowNetErrorPanel();
  107. }
  108. if (ListenDlg.Instance && ListenDlg.Instance.gameObject.activeSelf)
  109. {
  110. ListenDlg.Instance.ErrorToGuaduan();
  111. }
  112. WSHandler.roomRtcCloes();
  113. WSHandler.clientClosed();
  114. if (ShowRoom.Instance && ShowRoom.Instance.gameObject.activeSelf)
  115. {
  116. ShowRoom.Instance.NetErrorExitRoom();
  117. }
  118. TimerMgr.Instance.CreateTimer(() =>
  119. {
  120. if (RemoteRtc.Instance)
  121. {
  122. RemoteRtc.Instance.InitCenter();
  123. }
  124. }, 1f);
  125. }
  126. public static void clientClosed()
  127. {
  128. _client.CloseWebSocket();
  129. GameObject.Destroy(_client.gameObject);
  130. }
  131. public static string _roomid;
  132. public static void roomRtcinit(string roomid)
  133. {
  134. _roomid = roomid;
  135. GameObject prefab = (GameObject)Resources.Load("websocket");
  136. GameObject websocket = GameObject.Instantiate(prefab);
  137. _roomRtcClient = websocket.GetComponent<WebSocketClient>();
  138. _roomRtcClient.OnWebErrorClose += OnRoomRtcWebErrorClosed;
  139. _roomRtcClient.OnMessaged += OnRoomRtcMessageReceived;
  140. _roomRtcClient.Connect(NetWorkHeadersConfig.Remote_SocketRtc_url + "unionId=" + UserInfo.UnionId+ "&token=" + UserInfo.User_Token + "&roomId="+ _roomid, "音视频");
  141. _roomRtcClient._webSocket.OnError += OnRtcError;
  142. _roomRtcClient._webSocket.OnOpen += OnrtcOpen;
  143. _roomRtcClient._webSocket.OnMessage += OnRTCMessage;
  144. }
  145. private static void OnRtcError(WebSocket ws, Exception ex)
  146. {
  147. if (PopUpInfo.Instance)
  148. {
  149. PopUpInfo.Instance.ShowNetErrorPanel();
  150. }
  151. roomRtcCloes();
  152. if (_client != null && _client._webSocket != null)
  153. {
  154. WSHandler.clientClosed();
  155. ShowRoom.Instance.NetErrorExitRoom();
  156. TimerMgr.Instance.CreateTimer(() =>
  157. {
  158. if (RemoteRtc.Instance)
  159. {
  160. RemoteRtc.Instance.InitCenter();
  161. }
  162. }, 1f);
  163. }
  164. }
  165. public static void OnRoomRtcWebErrorClosed(string message)
  166. {
  167. if (PopUpInfo.Instance)
  168. {
  169. PopUpInfo.Instance.ShowNetErrorPanel();
  170. }
  171. roomRtcCloes();
  172. if (_client != null && _client._webSocket != null)
  173. {
  174. WSHandler.clientClosed();
  175. ShowRoom.Instance.NetErrorExitRoom();
  176. TimerMgr.Instance.CreateTimer(() =>
  177. {
  178. if (RemoteRtc.Instance)
  179. {
  180. RemoteRtc.Instance.InitCenter();
  181. }
  182. }, 1f);
  183. }
  184. }
  185. private static void OnRTCMessage(WebSocket webSocket, string message)
  186. {
  187. if (SCRtcFactory.Instance != null)
  188. {
  189. JsonData data = JsonMapper.ToObject(message);
  190. if (data.Keys.Contains("id") && data["id"].ToString() == WSRtcHandler.produceid)
  191. {
  192. SCRtcFactory.Instance.mSCRtcHandle.setProduce(data["data"]["id"].ToString());
  193. }
  194. else if (data.Keys.Contains("request"))
  195. {
  196. Rtc.RtcResponse(data);
  197. }
  198. }
  199. }
  200. public static void OnrtcOpen(WebSocket ws)
  201. {
  202. RemoteRtc.Instance.start(_roomid);
  203. }
  204. public static void roomRtcCloes()
  205. {
  206. try
  207. {
  208. if (_roomRtcClient)
  209. {
  210. _roomRtcClient.CloseWebSocket();
  211. }
  212. if (_roomRtcClient.gameObject)
  213. {
  214. GameObject.Destroy(_roomRtcClient.gameObject);
  215. }
  216. RemoteRtc.Instance.Close();
  217. }
  218. catch
  219. {
  220. // UnityLog.LogError("退出房间异常"+ _roomRtcClient);
  221. }
  222. }
  223. public static void SendRoomMessage(JsonData data)
  224. {
  225. SendRoomMessage(data.ToJson());
  226. }
  227. public static void SendRoomMessage(string data)
  228. {
  229. UnityLog.Log("SendRoomMessage===>" + data, 4);
  230. if (_roomClient != null)
  231. _roomClient.Send(data);
  232. }
  233. public static void SendRtcMessage(JsonData data)
  234. {
  235. UnityLog.Log("SendRtcMessage===>" + data.ToJson(), 1);
  236. string message = data.ToJson();
  237. if (_roomRtcClient != null)
  238. _roomRtcClient.Send(message);
  239. }
  240. public static void SendMessage(JsonData data)
  241. {
  242. UnityLog.Log("SendMessage===>" + data.ToJson(), 1);
  243. string message = data.ToJson();
  244. if (_client != null)
  245. _client.Send(message);
  246. }
  247. private static JsonData message_data = new JsonData();
  248. public static void OnMessageReceived(string message)
  249. {
  250. UnityLog.Log("SocketMessage===>" + message, 1);
  251. message_data.Clear();
  252. message_data = JsonMapper.ToObject(message);
  253. Office.OnMessageReceived(message_data);
  254. }
  255. public static void OnRoomRtcMessageReceived(string message)
  256. {
  257. UnityLog.Log("SocketRoomRtcMessage===>" + message, 1);
  258. message_data.Clear();
  259. message_data = JsonMapper.ToObject(message);
  260. Rtc.OnMessageReceived(message_data);
  261. }
  262. }