using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; using System; using XRTool.Util; using ShadowStudio.UI; using ShadowStudio.Model; using UnityEngine.SceneManagement; using BestHTTP.WebSocket; using static ScenesManager; using static ShowLogin; public class WSHandler { public static WebSocketClient _client; public static WebSocketClient _roomClient; public static WebSocketClient _roomRtcClient; private static WSOfficeHandler _Office; private static WSRoomHandler _room; private static WSRtcHandler _Rtc; private static WSGlobalHandler _global; private static WSPaintingHandler _painting; private static WSRtcHandler _webRtc; public static WSRtcHandler Rtc { get { if (_Rtc == null) { _Rtc = new WSRtcHandler(); } return _Rtc; } } public static WSOfficeHandler Office { get { if (_Office == null) { _Office = new WSOfficeHandler(); } return _Office; } } public static WSRoomHandler Room { get { if (_room == null) { _room = new WSRoomHandler(); } return _room; } } public static WSRtcHandler WebRtc { get { if (_webRtc == null) { _webRtc = new WSRtcHandler(); } return _webRtc; } } public static void init() { GameObject prefab = (GameObject)Resources.Load("websocket"); GameObject websocket = GameObject.Instantiate(prefab); _client = websocket.GetComponent(); _client.OnMessaged += OnMessageReceived; _client.OnWebErrorClose += OnWebErrorClosed; _client.Connect(NetWorkHeadersConfig.Remote_SocketHall_url+"token=" + UserInfo.User_Token+"&unionId="+UserInfo.UnionId, "大厅"); _client._webSocket.OnError += OnError; } private static void OnError(WebSocket ws, Exception ex) { if (PopUpInfo.Instance) { PopUpInfo.Instance.ShowNetErrorPanel(); } if (ListenDlg.Instance && ListenDlg.Instance.gameObject.activeSelf) { ListenDlg.Instance.ErrorToGuaduan(); } WSHandler.roomRtcCloes(); WSHandler.clientClosed(); if (ShowRoom.Instance && ShowRoom.Instance.gameObject.activeSelf) { ShowRoom.Instance.NetErrorExitRoom(); } TimerMgr.Instance.CreateTimer(() => { if (RemoteRtc.Instance) { RemoteRtc.Instance.InitCenter(); } }, 1f); } private static void OnWebErrorClosed(string data) { if (PopUpInfo.Instance) { PopUpInfo.Instance.ShowNetErrorPanel(); } if (ListenDlg.Instance && ListenDlg.Instance.gameObject.activeSelf) { ListenDlg.Instance.ErrorToGuaduan(); } WSHandler.roomRtcCloes(); WSHandler.clientClosed(); if (ShowRoom.Instance && ShowRoom.Instance.gameObject.activeSelf) { ShowRoom.Instance.NetErrorExitRoom(); } TimerMgr.Instance.CreateTimer(() => { if (RemoteRtc.Instance) { RemoteRtc.Instance.InitCenter(); } }, 1f); } public static void clientClosed() { _client.CloseWebSocket(); GameObject.Destroy(_client.gameObject); } public static string _roomid; public static void roomRtcinit(string roomid) { _roomid = roomid; GameObject prefab = (GameObject)Resources.Load("websocket"); GameObject websocket = GameObject.Instantiate(prefab); _roomRtcClient = websocket.GetComponent(); _roomRtcClient.OnWebErrorClose += OnRoomRtcWebErrorClosed; _roomRtcClient.OnMessaged += OnRoomRtcMessageReceived; _roomRtcClient.Connect(NetWorkHeadersConfig.Remote_SocketRtc_url + "unionId=" + UserInfo.UnionId+ "&token=" + UserInfo.User_Token + "&roomId="+ _roomid, "音视频"); _roomRtcClient._webSocket.OnError += OnRtcError; _roomRtcClient._webSocket.OnOpen += OnrtcOpen; _roomRtcClient._webSocket.OnMessage += OnRTCMessage; } private static void OnRtcError(WebSocket ws, Exception ex) { if (PopUpInfo.Instance) { PopUpInfo.Instance.ShowNetErrorPanel(); } roomRtcCloes(); if (_client != null && _client._webSocket != null) { WSHandler.clientClosed(); ShowRoom.Instance.NetErrorExitRoom(); TimerMgr.Instance.CreateTimer(() => { if (RemoteRtc.Instance) { RemoteRtc.Instance.InitCenter(); } }, 1f); } } public static void OnRoomRtcWebErrorClosed(string message) { if (PopUpInfo.Instance) { PopUpInfo.Instance.ShowNetErrorPanel(); } roomRtcCloes(); if (_client != null && _client._webSocket != null) { WSHandler.clientClosed(); ShowRoom.Instance.NetErrorExitRoom(); TimerMgr.Instance.CreateTimer(() => { if (RemoteRtc.Instance) { RemoteRtc.Instance.InitCenter(); } }, 1f); } } private static void OnRTCMessage(WebSocket webSocket, string message) { if (SCRtcFactory.Instance != null) { JsonData data = JsonMapper.ToObject(message); if (data.Keys.Contains("id") && data["id"].ToString() == WSRtcHandler.produceid) { SCRtcFactory.Instance.mSCRtcHandle.setProduce(data["data"]["id"].ToString()); } else if (data.Keys.Contains("request")) { Rtc.RtcResponse(data); } } } public static void OnrtcOpen(WebSocket ws) { RemoteRtc.Instance.start(_roomid); } public static void roomRtcCloes() { try { if (_roomRtcClient) { _roomRtcClient.CloseWebSocket(); } if (_roomRtcClient.gameObject) { GameObject.Destroy(_roomRtcClient.gameObject); } RemoteRtc.Instance.Close(); } catch { // UnityLog.LogError("退出房间异常"+ _roomRtcClient); } } public static void SendRoomMessage(JsonData data) { SendRoomMessage(data.ToJson()); } public static void SendRoomMessage(string data) { UnityLog.Log("SendRoomMessage===>" + data, 4); if (_roomClient != null) _roomClient.Send(data); } public static void SendRtcMessage(JsonData data) { UnityLog.Log("SendRtcMessage===>" + data.ToJson(), 1); string message = data.ToJson(); if (_roomRtcClient != null) _roomRtcClient.Send(message); } public static void SendMessage(JsonData data) { UnityLog.Log("SendMessage===>" + data.ToJson(), 1); string message = data.ToJson(); if (_client != null) _client.Send(message); } private static JsonData message_data = new JsonData(); public static void OnMessageReceived(string message) { UnityLog.Log("SocketMessage===>" + message, 1); message_data.Clear(); message_data = JsonMapper.ToObject(message); Office.OnMessageReceived(message_data); } public static void OnRoomRtcMessageReceived(string message) { UnityLog.Log("SocketRoomRtcMessage===>" + message, 1); message_data.Clear(); message_data = JsonMapper.ToObject(message); Rtc.OnMessageReceived(message_data); } }