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 BeinLab.Util; using BestHTTP.WebSocket; using ShadowStudio.Tool; using Studio.WebSocket.Message; public class WSHandler { public static WebSocketClient _client; public static WebSocketClient _roomClient; public static WebSocketClient _roomRtcClient; private static WSUserHandler _user; 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 WSUserHandler User { get { if (_user == null) { _user = new WSUserHandler(); } return _user; } } 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(SpaceHttpAction.Instance.lobbyUrl,"大厅"); _client._webSocket.OnError += OnError; } private static void OnError(WebSocket ws, Exception ex) { clientClosed(); Debug.Log("OnErrorOnErrorOnErrorOnErrorOnErrorOnError"+ ex.Message); CommonMethod.ShowTextTusi(); if (TuSi.Instance) { TuSi.Instance.ShowTextContent("大厅服务器网络不佳,正在重连。"); //LanguageMgr.Instance.GetMessage("1056").Message } // CommonMethod.ShowPopUp(PopType.Normal, LanguageMgr.Instance.GetMessage("1059").Message, LanguageMgr.Instance.GetMessage("1104").Message, () => // { LoadLoginSence(); }, LanguageMgr.Instance.GetMessage("1066").Message); TimerMgr.Instance.CreateTimer(()=> { init(); },1f); } private static void OnWebErrorClosed(string data) { clientClosed(); CommonMethod.ShowTextTusi(); if (TuSi.Instance) { TuSi.Instance.ShowTextContent("大厅服务器网络不佳,正在重连。"); //LanguageMgr.Instance.GetMessage("1056").Message } // CommonMethod.ShowPopUp(PopType.Normal, LanguageMgr.Instance.GetMessage("1059").Message, LanguageMgr.Instance.GetMessage("1104").Message, () => // { LoadLoginSence(); }, LanguageMgr.Instance.GetMessage("1066").Message); TimerMgr.Instance.CreateTimer(() => { init(); }, 1f); } public static void clientClosed() { if (_client) { _client.CloseWebSocket(); GameObject.Destroy(_client.gameObject); } } public static string _roomid; public static void roominit(string roomid) { _roomid = roomid; GameObject prefab = (GameObject)Resources.Load("websocket"); GameObject websocket = GameObject.Instantiate(prefab); _roomClient = websocket.GetComponent(); _roomClient.OnWebErrorClose += OnRoomWebErrorClosed; _roomClient.OnMessaged += OnRoomMessageReceived; _roomClient.Connect(SpaceHttpAction.Instance.roomUrl + UserInfo.User_Token + "&roomId=" + roomid,"房间"); } public static void roomRtcinit() { // AgoraVideoAudioManager.Instance.JoinChannel(); Debug.Log("rtc==============================>"); GameObject prefab = (GameObject)Resources.Load("websocket"); GameObject websocket = GameObject.Instantiate(prefab); _roomRtcClient = websocket.GetComponent(); _roomRtcClient.OnWebErrorClose += OnRoomRtcWebErrorClosed; _roomRtcClient.OnMessaged += OnRoomRtcMessageReceived; // // // _roomRtcClient.Connect("wss://apitest.shadowcreator.com:6443/?roomId=" + _roomid); // // _roomRtcClient.Connect("wss://cgdemo.shadowcreator.com/rtc/?roomId=" + _roomid, "音视频"); // _roomRtcClient.Connect(BuildConfig.Instance.rtcUrl+"?roomId=" + _roomid,"音视频"); //// _roomRtcClient.Connect("wss://api.shadowcreator.com:443/rtcwss/?roomId=" + _roomid); // _roomRtcClient._webSocket.OnOpen += OnrtcOpen; // _roomRtcClient._webSocket.OnMessage += OnRTCMessage; } 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") && data.Keys.Contains("method") && data["method"].ToString()== "newConsumer") { } else if (data.Keys.Contains("request")) { Rtc.RtcResponse(data); } } } public static void OnrtcOpen(WebSocket ws) { // StudioRtc.Instance.start(_roomid); } public static void OnRoomRtcMessageReceived(string message) { UnityLog.Instance.Log("SocketRoomRtcMessage===>" + message, 1); // SCRtcFactory.Instance.mSCRtcHandle.onSocketMesseage(message); message_data.Clear(); message_data = JsonMapper.ToObject(message); Rtc.OnMessageReceived(message_data); // message_data.Clear(); // message_data = JsonMapper.ToObject(message); // Room.OnMessageReceived(message_data, message); } public static void roomRtcCloes() { try { //if (StudioRtc.Instance) //{ // StudioRtc.Instance.Close(); //} //if (_roomRtcClient) //{ // _roomRtcClient.CloseWebSocket(); // GameObject.Destroy(_roomRtcClient.gameObject); //} } catch { // UnityLog.Instance.LogError("退出房间异常"+ _roomRtcClient); } } public static void OnRoomRtcWebErrorClosed(string message) { roomCloes(); Debug.Log("OnRoomRtcWebErrorClosedOnRoomRtcWebErrorClosedOnRoomRtcWebErrorClosedOnRoomRtcWebErrorClosedOnRoomRtcWebErrorClosed"); CommonMethod.ShowPopUp(PopType.Normal, LanguageMgr.Instance.GetMessage("1059").Message, LanguageMgr.Instance.GetMessage("1104").Message, () => { LoadLoginSence(); }, LanguageMgr.Instance.GetMessage("1066").Message); } public static void LoadLoginSence() { roomCloes(); clientClosed(); if (BoardDlg.Instance != null) { BoardDlg.Instance.DestoryARcamera(); } SceneManager.LoadScene("Login"); } public static void roomCloes() { if(_roomClient) { _roomClient.CloseWebSocket(); GameObject.Destroy(_roomClient.gameObject); } // roomRtcCloes(); } public static void OnRoomWebErrorClosed(string message) { roomCloes(); Debug.Log("OnRoomWebErrorClosedOnRoomWebErrorClosedOnRoomWebErrorClosedOnRoomWebErrorClosedOnRoomWebErrorClosedOnRoomWebErrorClosed"+ message); CommonMethod.ShowTextTusi(); if (TuSi.Instance) { TuSi.Instance.ShowTextContent("房间服务器网络不佳,正在重连。"); //LanguageMgr.Instance.GetMessage("1056").Message } TimerMgr.Instance.CreateTimer(() => { roominit(_roomid); WSHandler.Room.OnInit -= onRoomInit; WSHandler.Room.OnInit += onRoomInit; }, 1f); } private static void onRoomInit(JsonData data) { WSHandler.Room.OnJoinRoom -= onJoinRoom; WSHandler.Room.OnJoinRoom += onJoinRoom; WSHandler.Room.JoinRoom(CommonMethod.roomConfig.Id, CommonMethod.roomConfig.Room_password); // roomRtcinit(); } private static void onJoinRoom(List goods, List peers, List scenes, SceneConfig currentScene) { CommonMethod.IsReceive = false; } public static void OnRoomMessageReceived(string message) { // UnityLog.Instance.Log("SocketRoomMessage===>" + message, 4); message_data.Clear(); message_data = JsonMapper.ToObject(message); Room.OnMessageReceived(message_data, message); } public static void SendRoomMessage(JsonData data) { SendRoomMessage(data.ToJson()); } public static void SendRoomMessage(string data) { Debug.Log("SendRoomMessage===>" + data); // UnityLog.Instance.Log("SendRoomMessage===>" + data, 4); if (_roomClient != null) _roomClient.Send(data); } public static void SendRtcMessage(JsonData data) { UnityLog.Instance.Log("SendRtcMessage===>" + data.ToJson(), 1); string message = data.ToJson(); if (_roomRtcClient != null) _roomRtcClient.Send(message); } public static void SendMessage(JsonData data) { UnityLog.Instance.Log("SendMessage===>" + data.ToJson(), 4); string message = data.ToJson(); if (_client != null) _client.Send(message); } private static JsonData message_data = new JsonData(); public static void OnMessageReceived(string message) { // Debug.Log("SocketMessage===>" + message); UnityLog.Instance.Log("SocketMessage===>" + message, 4); //message_data.Clear(); //message_data = JsonMapper.ToObject(message); User.OnMessageReceived(message); } }