WSOfficeHandler.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;
  5. using XRTool.Util;
  6. using System;
  7. public class WSOfficeHandler{
  8. public delegate void OnInitDelegate(JsonData data);
  9. public delegate void OnSearchRoomDelegate(JsonData data);
  10. public delegate void OnGetSelfRoomListDelegate(JsonData data);
  11. public delegate void OnRoomChangeDelegate(JsonData data);
  12. public delegate void OnJionRoomDelegate(JsonData data);
  13. public delegate void OnGetRoomIdDelegate(JsonData data);
  14. public delegate void OnJoinRoomDelegate(JsonData data);
  15. public OnInitDelegate OnInit;
  16. public OnSearchRoomDelegate OnSearchRoom;
  17. public OnGetSelfRoomListDelegate OnGetSelfRoomList;
  18. public OnGetRoomIdDelegate OnGetRoomIdReveived;
  19. public OnJoinRoomDelegate OnJoinRoomReveived;
  20. public OnRoomChangeDelegate OnRoomChange;
  21. public event Action<JsonData> OnUserSearch;
  22. public event Action<JsonData> OnInviteUsers;
  23. public event Action<JsonData> OnRefuse;
  24. public event Action<JsonData> OnTimeout;
  25. public event Action<JsonData> OnInvitee;
  26. public event Action<JsonData> OnCallRecordsList;
  27. public event Action<JsonData> OnAcceptInviteSuccess;
  28. public event Action<JsonData> OnRefuseInviteSuccess;
  29. private Dictionary<string,string> idList = new Dictionary<string, string>();
  30. public void GetRoomID()
  31. {
  32. JsonData message = new JsonData();
  33. JsonData data = new JsonData();
  34. message["request"] = true;
  35. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  36. message["method"] = "getRoomId";
  37. WSHandler.SendMessage(message);
  38. CommonMethod.ShowNetLoading(WSHandler._client.name, "获取房间号...");
  39. idList.Add(message["id"].ToString(), message["method"].ToString());
  40. }
  41. public void GetRecordsList()
  42. {
  43. JsonData message = new JsonData();
  44. message["request"] = true;
  45. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  46. message["method"] = "callRecordsList";
  47. WSHandler.SendMessage(message);
  48. idList.Add(message["id"].ToString(), message["method"].ToString());
  49. }
  50. public void UserSearch(string search)
  51. {
  52. JsonData message = new JsonData();
  53. JsonData data = new JsonData();
  54. message["request"] = true;
  55. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  56. message["method"] = "userSearch";
  57. data["search"] = search;
  58. message["data"] = data;
  59. WSHandler.SendMessage(message);
  60. CommonMethod.ShowNetLoading(WSHandler._client.name, "搜索中...");
  61. idList.Add(message["id"].ToString(), message["method"].ToString());
  62. }
  63. //邀请用户
  64. public void InviteUsers(string roomId, List<InviteUserConfig> unionIds)
  65. {
  66. JsonData message = new JsonData();
  67. JsonData data = new JsonData();
  68. message["request"] = true;
  69. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  70. message["method"] = "inviteUsers";
  71. data["roomId"] = roomId;
  72. data["users"] = new JsonData();
  73. for (int i = 0; i < unionIds.Count; i++)
  74. {
  75. if (!string.IsNullOrEmpty(unionIds[i].UnionId))
  76. {
  77. JsonData user = new JsonData();
  78. user["unionId"] = unionIds[i].UnionId;
  79. data["users"].Add(user);
  80. }
  81. }
  82. message["data"] = data;
  83. WSHandler.SendMessage(message);
  84. CommonMethod.ShowNetLoading(WSHandler._client.name, "邀请中...");
  85. idList.Add(message["id"].ToString(), message["method"].ToString());
  86. }
  87. public void ChangeUserType(string type)
  88. {
  89. JsonData message = new JsonData();
  90. message["notification"] = true;
  91. message["method"] = type;
  92. WSHandler.SendMessage(message);
  93. }
  94. //接受邀请
  95. public void AcceptInvite()
  96. {
  97. JsonData message = new JsonData();
  98. message["request"] = true;
  99. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  100. message["method"] = "invite:accept";
  101. WSHandler.SendMessage(message);
  102. CommonMethod.ShowNetLoading(WSHandler._client.name, "接受邀请...");
  103. idList.Add(message["id"].ToString(), message["method"].ToString());
  104. }
  105. //拒绝邀请
  106. public void RefuseInvite()
  107. {
  108. JsonData message = new JsonData();
  109. message["request"] = true;
  110. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  111. message["method"] = "invite:refuse";
  112. WSHandler.SendMessage(message);
  113. CommonMethod.ShowNetLoading(WSHandler._client.name, "拒绝邀请...");
  114. idList.Add(message["id"].ToString(), message["method"].ToString());
  115. }
  116. public void JoinRoom(string roomId)
  117. {
  118. JsonData message = new JsonData();
  119. JsonData data = new JsonData();
  120. message["request"] = true;
  121. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  122. message["method"] = "joinRoom";
  123. message["data"] = new JsonData();
  124. message["data"]["roomId"] = roomId;
  125. WSHandler.SendMessage(message);
  126. CommonMethod.ShowNetLoading(WSHandler._client.name, "检测房间号...");
  127. idList.Add(message["id"].ToString(), message["method"].ToString());
  128. }
  129. public void OnMessageReceived(JsonData data)
  130. {
  131. if (data.Keys.Contains("response"))
  132. {
  133. CommonMethod.HideNetLoading(WSHandler._client.name);
  134. }
  135. if (data.Keys.Contains("errorCode"))
  136. {
  137. CommonMethod.HideLoading();
  138. return;
  139. }
  140. if (data.Keys.Contains("method"))
  141. {
  142. switch (data["method"].ToString())
  143. {
  144. case "connect":
  145. InitReceived(data);
  146. break;
  147. case "repeat":
  148. //ShowLogin.Instance.loginType = LoginType.INIT;
  149. //ScenesManager.Instance.showWindow(SceneType.ShowLogin);
  150. //PopUpInfo.Instance.showPublic(PopUpInfo.PopType.PopOk, RtcStrConfig.otherLogin,"返回登录");
  151. WSHandler.roomRtcCloes();
  152. WSHandler.clientClosed();
  153. break;
  154. case "invite:refuse":
  155. OnRefuse?.Invoke(data);
  156. break;
  157. case "invite:timeout":
  158. OnTimeout?.Invoke(data);
  159. break;
  160. case "invitee":
  161. OnInvitee?.Invoke(data);
  162. break;
  163. default:
  164. break;
  165. }
  166. }
  167. if (data.Keys.Contains("id"))
  168. {
  169. if (idList.ContainsKey(data["id"].ToString()))
  170. {
  171. switch (idList[data["id"].ToString()])
  172. {
  173. case "selfRoomList":
  174. GetSelfRoomListReveived(data);
  175. break;
  176. case "searchRoom":
  177. OnSearchRoomReveived(data);
  178. break;
  179. case "getRoomId":
  180. OnGetRoomIdReveived(data);
  181. break;
  182. case "joinRoom":
  183. OnJoinRoomReveived(data);
  184. break;
  185. case "userSearch":
  186. OnUserSearch?.Invoke(data);
  187. break;
  188. case "inviteUsers":
  189. OnInviteUsers?.Invoke(data);
  190. break;
  191. case "callRecordsList":
  192. OnCallRecordsList?.Invoke(data);
  193. break;
  194. case "invite:refuse":
  195. OnRefuseInviteSuccess?.Invoke(data);
  196. break;
  197. case "invite:accept":
  198. OnAcceptInviteSuccess?.Invoke(data);
  199. break;
  200. default:
  201. break;
  202. }
  203. idList.Remove(data["id"].ToString());
  204. }
  205. }
  206. }
  207. private void InitReceived(JsonData data)
  208. {
  209. if (OnInit != null)
  210. {
  211. OnInit(data);
  212. }
  213. }
  214. public void GetSelfRoomListReveived(JsonData data)
  215. {
  216. if (OnGetSelfRoomList != null)
  217. {
  218. OnGetSelfRoomList(data);
  219. }
  220. }
  221. public void OnSearchRoomReveived(JsonData data)
  222. {
  223. if (OnSearchRoom != null)
  224. {
  225. OnSearchRoom(data);
  226. }
  227. }
  228. }