123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- 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<string,string> idList = new Dictionary<string, string>();
- 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<BaseWebSocketMessage>(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<ConnectNotificationMessage>(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<SelfRoomListResponseMessage>(message));
- break;
- case "searchRoom":
- OnSearchRoomReveived(JsonUtility.FromJson<SearchRoomResponseMessage>(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);
- }
- }
- }
|