using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; using XRTool.Util; using ShadowStudio.UI; using Studio.WebSocket.Message; public class WSUserHandler { public delegate void OnInitDelegate(ConnectNotificationMessage data); public delegate void OnSearchRoomDelegate(SearchRoomResponseMessage data); public delegate void OnGetSelfRoomListDelegate(SelfRoomListResponseMessage netRoomListData); public delegate void OnRoomChangeDelegate(JsonData data); public delegate void OnJionRoomDelegate(JsonData data); public OnInitDelegate OnInit; public OnSearchRoomDelegate OnSearchRoom; public OnGetSelfRoomListDelegate OnGetSelfRoomList; public OnRoomChangeDelegate OnRoomChange; private Dictionary idList = new Dictionary(); public void GetSelfRoomList() { JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = Random.Range(10000000, 99999999); message["method"] = "selfRoomList"; data["token"] = UserInfo.User_Token; message["data"] = data; WSHandler.SendMessage(message); CommonMethod.ShowNetLoading(WSHandler._client.name,"获取房间列表中..."); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void SearchRoom(string searchContent) { JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = Random.Range(10000000, 99999999); message["method"] = "searchRoom"; data["search"] = searchContent; data["token"] = UserInfo.User_Token; message["data"] = data; WSHandler.SendMessage(message); CommonMethod.ShowNetLoading(WSHandler._client.name,"查询房间中...."); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void OnMessageReceived(string message) { BaseWebSocketMessage baseWebSocketMessage = JsonUtility.FromJson(message); if (baseWebSocketMessage.response) { CommonMethod.HideNetLoading(WSHandler._client.name); } if (baseWebSocketMessage.errorCode.code!=-1) { CommonMethod.HideLoading(); CommonMethod.ShowTextTusi(); if (TuSi.Instance) { TuSi.Instance.ShowTextContent(baseWebSocketMessage.errorCode.message); } return; } if (!string.IsNullOrEmpty(baseWebSocketMessage.method)) { switch (baseWebSocketMessage.method) { case "connect": Debug.Log("连接Connect。。。"); InitReceived(JsonUtility.FromJson(message)); break; case "roomChange": //OnRoomChangeReveived(data); break; default: break; } } if (baseWebSocketMessage.id!=-1) { if (idList.ContainsKey(baseWebSocketMessage.id.ToString())) { switch (idList[baseWebSocketMessage.id.ToString()]) { case "selfRoomList": GetSelfRoomListReveived(JsonUtility.FromJson(message)); break; case "searchRoom": OnSearchRoomReveived(JsonUtility.FromJson(message)); break; default: break; } idList.Remove(baseWebSocketMessage.id.ToString()); } } } private void InitReceived(ConnectNotificationMessage message) { if (OnInit != null) { OnInit(message); } } public void GetSelfRoomListReveived(SelfRoomListResponseMessage data) { if (OnGetSelfRoomList != null) { OnGetSelfRoomList(data); } } public void OnSearchRoomReveived(SearchRoomResponseMessage data) { if (OnSearchRoom != null) { OnSearchRoom(data); } } public void OnRoomChangeReveived(JsonData data) { if (OnRoomChange != null) { OnRoomChange(data); } } }