WSHandler.cs 7.9 KB

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