123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- 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<WebSocketClient>();
- _client.OnMessaged += OnMessageReceived;
- _client.OnWebErrorClose += OnWebErrorClosed;
- _client.Connect(BuildConfig.Instance.lobbyUrl,"大厅");
- _client._webSocket.OnError += OnError;
- }
- private static void OnError(WebSocket ws, Exception ex)
- {
- clientClosed();
- CommonMethod.ShowPopUp(PopType.Normal, LanguageMgr.Instance.GetMessage("1059").Message, LanguageMgr.Instance.GetMessage("1104").Message, () =>
- { LoadLoginSence(); }, LanguageMgr.Instance.GetMessage("1066").Message);
- }
- private static void OnWebErrorClosed(string data)
- {
- clientClosed();
- CommonMethod.ShowPopUp(PopType.Normal, LanguageMgr.Instance.GetMessage("1059").Message, LanguageMgr.Instance.GetMessage("1104").Message, () =>
- { LoadLoginSence(); }, LanguageMgr.Instance.GetMessage("1066").Message);
- }
- 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<WebSocketClient>();
- _roomClient.OnWebErrorClose += OnRoomWebErrorClosed;
- _roomClient.OnMessaged += OnRoomMessageReceived;
- _roomClient.Connect(BuildConfig.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<WebSocketClient>();
- _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();
- 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();
- CommonMethod.ShowPopUp(PopType.Normal, LanguageMgr.Instance.GetMessage("1059").Message, LanguageMgr.Instance.GetMessage("1104").Message, () =>
- { LoadLoginSence(); }, LanguageMgr.Instance.GetMessage("1066").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<GoodsInfo> goods, List<Peer> peers, List<SceneConfig> 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);
- }
- }
|