using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; using XRTool.Util; using System; public class WSOfficeHandler{ public delegate void OnInitDelegate(JsonData data); public delegate void OnSearchRoomDelegate(JsonData data); public delegate void OnGetSelfRoomListDelegate(JsonData data); public delegate void OnRoomChangeDelegate(JsonData data); public delegate void OnJionRoomDelegate(JsonData data); public delegate void OnGetRoomIdDelegate(JsonData data); public delegate void OnJoinRoomDelegate(JsonData data); public OnInitDelegate OnInit; public OnSearchRoomDelegate OnSearchRoom; public OnGetSelfRoomListDelegate OnGetSelfRoomList; public OnGetRoomIdDelegate OnGetRoomIdReveived; public OnJoinRoomDelegate OnJoinRoomReveived; public OnRoomChangeDelegate OnRoomChange; public event Action OnUserSearch; public event Action OnInviteUsers; public event Action OnRefuse; public event Action OnTimeout; public event Action OnInvitee; public event Action OnCallRecordsList; public event Action OnAcceptInviteSuccess; public event Action OnRefuseInviteSuccess; private Dictionary idList = new Dictionary(); public void GetRoomID() { JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "getRoomId"; WSHandler.SendMessage(message); CommonMethod.ShowNetLoading(WSHandler._client.name, "获取房间号..."); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void GetRecordsList() { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "callRecordsList"; WSHandler.SendMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void UserSearch(string search) { JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "userSearch"; data["search"] = search; message["data"] = data; WSHandler.SendMessage(message); CommonMethod.ShowNetLoading(WSHandler._client.name, "搜索中..."); idList.Add(message["id"].ToString(), message["method"].ToString()); } //邀请用户 public void InviteUsers(string roomId, List unionIds) { JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "inviteUsers"; data["roomId"] = roomId; data["users"] = new JsonData(); for (int i = 0; i < unionIds.Count; i++) { if (!string.IsNullOrEmpty(unionIds[i].UnionId)) { JsonData user = new JsonData(); user["unionId"] = unionIds[i].UnionId; data["users"].Add(user); } } message["data"] = data; WSHandler.SendMessage(message); CommonMethod.ShowNetLoading(WSHandler._client.name, "邀请中..."); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void ChangeUserType(string type) { JsonData message = new JsonData(); message["notification"] = true; message["method"] = type; WSHandler.SendMessage(message); } //接受邀请 public void AcceptInvite() { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "invite:accept"; WSHandler.SendMessage(message); CommonMethod.ShowNetLoading(WSHandler._client.name, "接受邀请..."); idList.Add(message["id"].ToString(), message["method"].ToString()); } //拒绝邀请 public void RefuseInvite() { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "invite:refuse"; WSHandler.SendMessage(message); CommonMethod.ShowNetLoading(WSHandler._client.name, "拒绝邀请..."); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void JoinRoom(string roomId) { JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "joinRoom"; message["data"] = new JsonData(); message["data"]["roomId"] = roomId; WSHandler.SendMessage(message); CommonMethod.ShowNetLoading(WSHandler._client.name, "检测房间号..."); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void OnMessageReceived(JsonData data) { if (data.Keys.Contains("response")) { CommonMethod.HideNetLoading(WSHandler._client.name); } if (data.Keys.Contains("errorCode")) { CommonMethod.HideLoading(); return; } if (data.Keys.Contains("method")) { switch (data["method"].ToString()) { case "connect": InitReceived(data); break; case "repeat": //ShowLogin.Instance.loginType = LoginType.INIT; //ScenesManager.Instance.showWindow(SceneType.ShowLogin); //PopUpInfo.Instance.showPublic(PopUpInfo.PopType.PopOk, RtcStrConfig.otherLogin,"返回登录"); WSHandler.roomRtcCloes(); WSHandler.clientClosed(); break; case "invite:refuse": OnRefuse?.Invoke(data); break; case "invite:timeout": OnTimeout?.Invoke(data); break; case "invitee": OnInvitee?.Invoke(data); break; default: break; } } if (data.Keys.Contains("id")) { if (idList.ContainsKey(data["id"].ToString())) { switch (idList[data["id"].ToString()]) { case "selfRoomList": GetSelfRoomListReveived(data); break; case "searchRoom": OnSearchRoomReveived(data); break; case "getRoomId": OnGetRoomIdReveived(data); break; case "joinRoom": OnJoinRoomReveived(data); break; case "userSearch": OnUserSearch?.Invoke(data); break; case "inviteUsers": OnInviteUsers?.Invoke(data); break; case "callRecordsList": OnCallRecordsList?.Invoke(data); break; case "invite:refuse": OnRefuseInviteSuccess?.Invoke(data); break; case "invite:accept": OnAcceptInviteSuccess?.Invoke(data); break; default: break; } idList.Remove(data["id"].ToString()); } } } private void InitReceived(JsonData data) { if (OnInit != null) { OnInit(data); } } public void GetSelfRoomListReveived(JsonData data) { if (OnGetSelfRoomList != null) { OnGetSelfRoomList(data); } } public void OnSearchRoomReveived(JsonData data) { if (OnSearchRoom != null) { OnSearchRoom(data); } } }