123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- 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<WebSocketClient>();
- _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<WebSocketClient>();
- _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);
- }
- }
|