WSHandler.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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(BuildConfig.Instance.lobbyUrl,"大厅");
  77. _client._webSocket.OnError += OnError;
  78. }
  79. private static void OnError(WebSocket ws, Exception ex)
  80. {
  81. clientClosed();
  82. CommonMethod.ShowPopUp(PopType.Normal, LanguageMgr.Instance.GetMessage("1059").Message, LanguageMgr.Instance.GetMessage("1104").Message, () =>
  83. { LoadLoginSence(); }, LanguageMgr.Instance.GetMessage("1066").Message);
  84. }
  85. private static void OnWebErrorClosed(string data)
  86. {
  87. clientClosed();
  88. CommonMethod.ShowPopUp(PopType.Normal, LanguageMgr.Instance.GetMessage("1059").Message, LanguageMgr.Instance.GetMessage("1104").Message, () =>
  89. { LoadLoginSence(); }, LanguageMgr.Instance.GetMessage("1066").Message);
  90. }
  91. public static void clientClosed()
  92. {
  93. if (_client)
  94. {
  95. _client.CloseWebSocket();
  96. GameObject.Destroy(_client.gameObject);
  97. }
  98. }
  99. public static string _roomid;
  100. public static void roominit(string roomid)
  101. {
  102. _roomid = roomid;
  103. GameObject prefab = (GameObject)Resources.Load("websocket");
  104. GameObject websocket = GameObject.Instantiate(prefab);
  105. _roomClient = websocket.GetComponent<WebSocketClient>();
  106. _roomClient.OnWebErrorClose += OnRoomWebErrorClosed;
  107. _roomClient.OnMessaged += OnRoomMessageReceived;
  108. _roomClient.Connect(BuildConfig.Instance.roomUrl + UserInfo.User_Token + "&roomId=" + roomid,"房间");
  109. }
  110. public static void roomRtcinit()
  111. {
  112. // AgoraVideoAudioManager.Instance.JoinChannel();
  113. Debug.Log("rtc==============================>");
  114. GameObject prefab = (GameObject)Resources.Load("websocket");
  115. GameObject websocket = GameObject.Instantiate(prefab);
  116. _roomRtcClient = websocket.GetComponent<WebSocketClient>();
  117. _roomRtcClient.OnWebErrorClose += OnRoomRtcWebErrorClosed;
  118. _roomRtcClient.OnMessaged += OnRoomRtcMessageReceived;
  119. //
  120. // // _roomRtcClient.Connect("wss://apitest.shadowcreator.com:6443/?roomId=" + _roomid);
  121. // // _roomRtcClient.Connect("wss://cgdemo.shadowcreator.com/rtc/?roomId=" + _roomid, "音视频");
  122. // _roomRtcClient.Connect(BuildConfig.Instance.rtcUrl+"?roomId=" + _roomid,"音视频");
  123. //// _roomRtcClient.Connect("wss://api.shadowcreator.com:443/rtcwss/?roomId=" + _roomid);
  124. // _roomRtcClient._webSocket.OnOpen += OnrtcOpen;
  125. // _roomRtcClient._webSocket.OnMessage += OnRTCMessage;
  126. }
  127. private static void OnRTCMessage(WebSocket webSocket, string message)
  128. {
  129. if (SCRtcFactory.Instance != null)
  130. {
  131. JsonData data = JsonMapper.ToObject(message);
  132. if (data.Keys.Contains("id") && data["id"].ToString() == WSRtcHandler.produceid)
  133. {
  134. SCRtcFactory.Instance.mSCRtcHandle.setProduce(data["data"]["id"].ToString());
  135. }
  136. else if(data.Keys.Contains("request") && data.Keys.Contains("method") && data["method"].ToString()== "newConsumer")
  137. {
  138. }
  139. else if (data.Keys.Contains("request"))
  140. {
  141. Rtc.RtcResponse(data);
  142. }
  143. }
  144. }
  145. public static void OnrtcOpen(WebSocket ws)
  146. {
  147. // StudioRtc.Instance.start(_roomid);
  148. }
  149. public static void OnRoomRtcMessageReceived(string message)
  150. {
  151. UnityLog.Instance.Log("SocketRoomRtcMessage===>" + message, 1);
  152. // SCRtcFactory.Instance.mSCRtcHandle.onSocketMesseage(message);
  153. message_data.Clear();
  154. message_data = JsonMapper.ToObject(message);
  155. Rtc.OnMessageReceived(message_data);
  156. // message_data.Clear();
  157. // message_data = JsonMapper.ToObject(message);
  158. // Room.OnMessageReceived(message_data, message);
  159. }
  160. public static void roomRtcCloes()
  161. {
  162. try
  163. {
  164. //if (StudioRtc.Instance)
  165. //{
  166. // StudioRtc.Instance.Close();
  167. //}
  168. //if (_roomRtcClient)
  169. //{
  170. // _roomRtcClient.CloseWebSocket();
  171. // GameObject.Destroy(_roomRtcClient.gameObject);
  172. //}
  173. }
  174. catch
  175. {
  176. // UnityLog.Instance.LogError("退出房间异常"+ _roomRtcClient);
  177. }
  178. }
  179. public static void OnRoomRtcWebErrorClosed(string message)
  180. {
  181. roomCloes();
  182. CommonMethod.ShowPopUp(PopType.Normal, LanguageMgr.Instance.GetMessage("1059").Message, LanguageMgr.Instance.GetMessage("1104").Message, () =>
  183. { LoadLoginSence(); }, LanguageMgr.Instance.GetMessage("1066").Message);
  184. }
  185. public static void LoadLoginSence()
  186. {
  187. roomCloes();
  188. clientClosed();
  189. if (BoardDlg.Instance != null)
  190. {
  191. BoardDlg.Instance.DestoryARcamera();
  192. }
  193. SceneManager.LoadScene("Login");
  194. }
  195. public static void roomCloes()
  196. {
  197. if(_roomClient)
  198. {
  199. _roomClient.CloseWebSocket();
  200. GameObject.Destroy(_roomClient.gameObject);
  201. }
  202. // roomRtcCloes();
  203. }
  204. public static void OnRoomWebErrorClosed(string message)
  205. {
  206. roomCloes();
  207. CommonMethod.ShowPopUp(PopType.Normal, LanguageMgr.Instance.GetMessage("1059").Message, LanguageMgr.Instance.GetMessage("1104").Message, () =>
  208. { LoadLoginSence(); }, LanguageMgr.Instance.GetMessage("1066").Message);
  209. /*
  210. TimerMgr.Instance.CreateTimer(() =>
  211. {
  212. roominit(_roomid);
  213. WSHandler.Room.OnInit -= onRoomInit;
  214. WSHandler.Room.OnInit += onRoomInit;
  215. }, 1f);*/
  216. }
  217. private static void onRoomInit(JsonData data)
  218. {
  219. WSHandler.Room.OnJoinRoom -= onJoinRoom;
  220. WSHandler.Room.OnJoinRoom += onJoinRoom;
  221. WSHandler.Room.JoinRoom(CommonMethod.roomConfig.Id, CommonMethod.roomConfig.Room_password);
  222. // roomRtcinit();
  223. }
  224. private static void onJoinRoom(List<GoodsInfo> goods, List<Peer> peers, List<SceneConfig> scenes, SceneConfig currentScene)
  225. {
  226. CommonMethod.IsReceive = false;
  227. }
  228. public static void OnRoomMessageReceived(string message)
  229. {
  230. // UnityLog.Instance.Log("SocketRoomMessage===>" + message, 4);
  231. message_data.Clear();
  232. message_data = JsonMapper.ToObject(message);
  233. Room.OnMessageReceived(message_data, message);
  234. }
  235. public static void SendRoomMessage(JsonData data)
  236. {
  237. SendRoomMessage(data.ToJson());
  238. }
  239. public static void SendRoomMessage(string data)
  240. {
  241. Debug.Log("SendRoomMessage===>" + data);
  242. // UnityLog.Instance.Log("SendRoomMessage===>" + data, 4);
  243. if (_roomClient != null)
  244. _roomClient.Send(data);
  245. }
  246. public static void SendRtcMessage(JsonData data)
  247. {
  248. UnityLog.Instance.Log("SendRtcMessage===>" + data.ToJson(), 1);
  249. string message = data.ToJson();
  250. if (_roomRtcClient != null)
  251. _roomRtcClient.Send(message);
  252. }
  253. public static void SendMessage(JsonData data)
  254. {
  255. UnityLog.Instance.Log("SendMessage===>" + data.ToJson(), 4);
  256. string message = data.ToJson();
  257. if (_client != null)
  258. _client.Send(message);
  259. }
  260. private static JsonData message_data = new JsonData();
  261. public static void OnMessageReceived(string message)
  262. {
  263. // Debug.Log("SocketMessage===>" + message);
  264. UnityLog.Instance.Log("SocketMessage===>" + message, 4);
  265. //message_data.Clear();
  266. //message_data = JsonMapper.ToObject(message);
  267. User.OnMessageReceived(message);
  268. }
  269. }