WSUserHandler.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;
  5. using XRTool.Util;
  6. using ShadowStudio.UI;
  7. using Studio.WebSocket.Message;
  8. public class WSUserHandler {
  9. public delegate void OnInitDelegate(ConnectNotificationMessage data);
  10. public delegate void OnSearchRoomDelegate(SearchRoomResponseMessage data);
  11. public delegate void OnGetSelfRoomListDelegate(SelfRoomListResponseMessage netRoomListData);
  12. public delegate void OnRoomChangeDelegate(JsonData data);
  13. public delegate void OnJionRoomDelegate(JsonData data);
  14. public OnInitDelegate OnInit;
  15. public OnSearchRoomDelegate OnSearchRoom;
  16. public OnGetSelfRoomListDelegate OnGetSelfRoomList;
  17. public OnRoomChangeDelegate OnRoomChange;
  18. private Dictionary<string,string> idList = new Dictionary<string, string>();
  19. public void GetSelfRoomList()
  20. {
  21. JsonData message = new JsonData();
  22. JsonData data = new JsonData();
  23. message["request"] = true;
  24. message["id"] = Random.Range(10000000, 99999999);
  25. message["method"] = "selfRoomList";
  26. data["token"] = UserInfo.User_Token;
  27. message["data"] = data;
  28. WSHandler.SendMessage(message);
  29. CommonMethod.ShowNetLoading(WSHandler._client.name,"获取房间列表中...");
  30. idList.Add(message["id"].ToString(), message["method"].ToString());
  31. }
  32. public void SearchRoom(string searchContent)
  33. {
  34. JsonData message = new JsonData();
  35. JsonData data = new JsonData();
  36. message["request"] = true;
  37. message["id"] = Random.Range(10000000, 99999999);
  38. message["method"] = "searchRoom";
  39. data["search"] = searchContent;
  40. data["token"] = UserInfo.User_Token;
  41. message["data"] = data;
  42. WSHandler.SendMessage(message);
  43. CommonMethod.ShowNetLoading(WSHandler._client.name,"查询房间中....");
  44. idList.Add(message["id"].ToString(), message["method"].ToString());
  45. }
  46. public void OnMessageReceived(string message)
  47. {
  48. BaseWebSocketMessage baseWebSocketMessage = JsonUtility.FromJson<BaseWebSocketMessage>(message);
  49. if (baseWebSocketMessage.response)
  50. {
  51. CommonMethod.HideNetLoading(WSHandler._client.name);
  52. }
  53. if (baseWebSocketMessage.errorCode.code!=-1)
  54. {
  55. CommonMethod.HideLoading();
  56. CommonMethod.ShowTextTusi();
  57. if (TuSi.Instance)
  58. {
  59. TuSi.Instance.ShowTextContent(baseWebSocketMessage.errorCode.message);
  60. }
  61. return;
  62. }
  63. if (!string.IsNullOrEmpty(baseWebSocketMessage.method))
  64. {
  65. switch (baseWebSocketMessage.method)
  66. {
  67. case "connect":
  68. Debug.Log("连接Connect。。。");
  69. InitReceived(JsonUtility.FromJson<ConnectNotificationMessage>(message));
  70. break;
  71. case "roomChange":
  72. //OnRoomChangeReveived(data);
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78. if (baseWebSocketMessage.id!=-1)
  79. {
  80. if (idList.ContainsKey(baseWebSocketMessage.id.ToString()))
  81. {
  82. switch (idList[baseWebSocketMessage.id.ToString()])
  83. {
  84. case "selfRoomList":
  85. GetSelfRoomListReveived(JsonUtility.FromJson<SelfRoomListResponseMessage>(message));
  86. break;
  87. case "searchRoom":
  88. OnSearchRoomReveived(JsonUtility.FromJson<SearchRoomResponseMessage>(message));
  89. break;
  90. default:
  91. break;
  92. }
  93. idList.Remove(baseWebSocketMessage.id.ToString());
  94. }
  95. }
  96. }
  97. private void InitReceived(ConnectNotificationMessage message)
  98. {
  99. if (OnInit != null)
  100. {
  101. OnInit(message);
  102. }
  103. }
  104. public void GetSelfRoomListReveived(SelfRoomListResponseMessage data)
  105. {
  106. if (OnGetSelfRoomList != null)
  107. {
  108. OnGetSelfRoomList(data);
  109. }
  110. }
  111. public void OnSearchRoomReveived(SearchRoomResponseMessage data)
  112. {
  113. if (OnSearchRoom != null)
  114. {
  115. OnSearchRoom(data);
  116. }
  117. }
  118. public void OnRoomChangeReveived(JsonData data)
  119. {
  120. if (OnRoomChange != null)
  121. {
  122. OnRoomChange(data);
  123. }
  124. }
  125. }