WSHandler.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;
  5. using System;
  6. using XRTool.Util;
  7. using UnityEngine.SceneManagement;
  8. using BestHTTP.WebSocket;
  9. public class WSHandler
  10. {
  11. public static WebSocketClient _client;
  12. public static WebSocketClient _roomClient;
  13. public static WebSocketClient _roomRtcClient;
  14. private static WSOfficeHandler _Office;
  15. private static WSRtcHandler _Rtc;
  16. private static WSRtcHandler _webRtc;
  17. public static WSRtcHandler Rtc
  18. {
  19. get
  20. {
  21. if (_Rtc == null)
  22. {
  23. _Rtc = new WSRtcHandler();
  24. }
  25. return _Rtc;
  26. }
  27. }
  28. public static WSOfficeHandler Office
  29. {
  30. get
  31. {
  32. if (_Office == null)
  33. {
  34. _Office = new WSOfficeHandler();
  35. }
  36. return _Office;
  37. }
  38. }
  39. public static WSRtcHandler WebRtc
  40. {
  41. get
  42. {
  43. if (_webRtc == null)
  44. {
  45. _webRtc = new WSRtcHandler();
  46. }
  47. return _webRtc;
  48. }
  49. }
  50. public static void init()
  51. {
  52. GameObject prefab = (GameObject)Resources.Load("websocket");
  53. GameObject websocket = GameObject.Instantiate(prefab);
  54. _client = websocket.GetComponent<WebSocketClient>();
  55. _client.OnMessaged += OnMessageReceived;
  56. _client.OnWebErrorClose += OnWebErrorClosed;
  57. _client.Connect(NetWorkHeadersConfig.Remote_SocketHall_url+"token=" + UserInfo.User_Token+"&unionId="+UserInfo.UnionId, "大厅");
  58. _client._webSocket.OnError += OnError;
  59. }
  60. private static void OnRtcError(WebSocket ws, Exception ex)
  61. {
  62. //PopUpInfo.Instance.showPublic(PopUpInfo.PopType.PopOk, RtcStrConfig.serverClose, "返回登陆", () => {
  63. // ShowLogin.Instance.loginType = LoginType.INIT;
  64. // ScenesManager.Instance.showWindow(SceneType.ShowLogin);
  65. //});
  66. WSHandler.clientClosed();
  67. WSHandler.roomRtcCloes();
  68. // PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, "服务器连接失败!");
  69. /*
  70. TimerMgr.Instance.CreateTimer(() =>
  71. {
  72. init();
  73. }, 1f);*/
  74. }
  75. private static void OnError(WebSocket ws, Exception ex)
  76. {
  77. _client.CloseWebSocket();
  78. GameObject.Destroy(_client.gameObject);
  79. WSHandler.roomRtcCloes();
  80. WSHandler.clientClosed();
  81. //PopUpInfo.Instance.showPublic(PopUpInfo.PopType.PopOk, RtcStrConfig.serverClose, "返回登陆", () => {
  82. // ShowLogin.Instance.loginType = LoginType.INIT ;
  83. // ScenesManager.Instance.showWindow(SceneType.ShowLogin);
  84. //});
  85. }
  86. private static void OnWebErrorClosed(string data)
  87. {
  88. _client.CloseWebSocket();
  89. GameObject.Destroy(_client.gameObject);
  90. WSHandler.roomRtcCloes();
  91. WSHandler.clientClosed();
  92. //PopUpInfo.Instance.showPublic(PopUpInfo.PopType.PopOk, RtcStrConfig.serverClose, "返回登陆", () => {
  93. // ShowLogin.Instance.loginType = LoginType.INIT;
  94. // ScenesManager.Instance.showWindow(SceneType.ShowLogin);
  95. //});
  96. }
  97. public static void clientClosed()
  98. {
  99. _client.CloseWebSocket();
  100. GameObject.Destroy(_client.gameObject);
  101. }
  102. public static string _roomid;
  103. public static void roomRtcinit(string roomid)
  104. {
  105. _roomid = roomid;
  106. GameObject prefab = (GameObject)Resources.Load("websocket");
  107. GameObject websocket = GameObject.Instantiate(prefab);
  108. _roomRtcClient = websocket.GetComponent<WebSocketClient>();
  109. _roomRtcClient.OnWebErrorClose += OnRoomRtcWebErrorClosed;
  110. _roomRtcClient.OnMessaged += OnRoomRtcMessageReceived;
  111. //
  112. // _roomRtcClient.Connect("wss://apitest.shadowcreator.com:6443/?roomId=" + _roomid);
  113. _roomRtcClient.Connect(NetWorkHeadersConfig.Remote_SocketRtc_url + "unionId=" + UserInfo.UnionId+ "&token=" + UserInfo.User_Token + "&roomId="+ _roomid, "音视频");
  114. _roomRtcClient._webSocket.OnError += OnRtcError;
  115. // _roomRtcClient.Connect("wss://cgdemo.shadowcreator.com/rtc/?roomId=" + _roomid, "音视频");
  116. // _roomRtcClient.Connect("wss://api.shadowcreator.com:443/rtcwss/?roomId=" + _roomid);
  117. _roomRtcClient._webSocket.OnOpen += OnrtcOpen;
  118. _roomRtcClient._webSocket.OnMessage += OnRTCMessage;
  119. }
  120. private static void OnRTCMessage(WebSocket webSocket, string message)
  121. {
  122. if (SCRtcFactory.Instance != null)
  123. {
  124. JsonData data = JsonMapper.ToObject(message);
  125. if (data.Keys.Contains("id") && data["id"].ToString() == WSRtcHandler.produceid)
  126. {
  127. SCRtcFactory.Instance.mSCRtcHandle.setProduce(data["data"]["id"].ToString());
  128. }
  129. else if (data.Keys.Contains("request"))
  130. {
  131. Rtc.RtcResponse(data);
  132. }
  133. }
  134. }
  135. public static void OnrtcOpen(WebSocket ws)
  136. {
  137. RemoteRtc.Instance.start(_roomid);
  138. }
  139. public static void roomRtcCloes()
  140. {
  141. try
  142. {
  143. if (_roomRtcClient)
  144. {
  145. _roomRtcClient.CloseWebSocket();
  146. }
  147. if (_roomRtcClient.gameObject)
  148. {
  149. GameObject.Destroy(_roomRtcClient.gameObject);
  150. }
  151. RemoteRtc.Instance.Close();
  152. }
  153. catch
  154. {
  155. // UnityLog.LogError("退出房间异常"+ _roomRtcClient);
  156. }
  157. }
  158. public static void OnRoomRtcWebErrorClosed(string message)
  159. {
  160. roomRtcCloes();
  161. /*
  162. TimerMgr.Instance.CreateTimer(() =>
  163. {
  164. roomRtcinit(_roomid);
  165. }, 1f);*/
  166. }
  167. public static void roomCloes()
  168. {
  169. _roomClient.CloseWebSocket();
  170. GameObject.Destroy(_roomClient.gameObject);
  171. roomRtcCloes();
  172. }
  173. public static void OnRoomWebErrorClosed(string message)
  174. {
  175. roomCloes();
  176. /*
  177. TimerMgr.Instance.CreateTimer(() =>
  178. {
  179. roominit(_roomid);
  180. WSHandler.Room.OnInit -= onRoomInit;
  181. WSHandler.Room.OnInit += onRoomInit;
  182. }, 1f);*/
  183. }
  184. public static void SendRtcMessage(JsonData data)
  185. {
  186. UnityLog.Log("SendRtcMessage===>" + data.ToJson(), 1);
  187. string message = data.ToJson();
  188. if (_roomRtcClient != null)
  189. _roomRtcClient.Send(message);
  190. }
  191. public static void SendMessage(JsonData data)
  192. {
  193. UnityLog.Log("SendMessage===>" + data.ToJson(), 1);
  194. string message = data.ToJson();
  195. if (_client != null)
  196. _client.Send(message);
  197. }
  198. private static JsonData message_data = new JsonData();
  199. public static void OnMessageReceived(string message)
  200. {
  201. UnityLog.Log("SocketMessage===>" + message, 1);
  202. message_data.Clear();
  203. message_data = JsonMapper.ToObject(message);
  204. Office.OnMessageReceived(message_data);
  205. }
  206. public static void OnRoomRtcMessageReceived(string message)
  207. {
  208. UnityLog.Log("SocketRoomRtcMessage===>" + message, 1);
  209. message_data.Clear();
  210. message_data = JsonMapper.ToObject(message);
  211. Rtc.OnMessageReceived(message_data);
  212. }
  213. }