WSOfficeHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 static ScenesManager;
  8. using static ShowLogin;
  9. using System;
  10. using rtc;
  11. public class WSOfficeHandler{
  12. public delegate void OnInitDelegate(JsonData data);
  13. public delegate void OnSearchRoomDelegate(JsonData data);
  14. public delegate void OnGetSelfRoomListDelegate(JsonData data);
  15. public delegate void OnRoomChangeDelegate(JsonData data);
  16. public delegate void OnJionRoomDelegate(JsonData data);
  17. public delegate void OnGetRoomIdDelegate(JsonData data);
  18. public delegate void OnJoinRoomDelegate(JsonData data);
  19. public OnInitDelegate OnInit;
  20. public OnSearchRoomDelegate OnSearchRoom;
  21. public OnGetSelfRoomListDelegate OnGetSelfRoomList;
  22. public OnGetRoomIdDelegate OnGetRoomIdReveived;
  23. public OnJoinRoomDelegate OnJoinRoomReveived;
  24. public OnRoomChangeDelegate OnRoomChange;
  25. public event Action<JsonData> OnUserSearch;
  26. public event Action<JsonData> OnInviteUsers;
  27. public event Action<JsonData> OnRefuse;
  28. public event Action<JsonData> OnTimeout;
  29. public event Action<JsonData> OnInvitee;
  30. public event Action<JsonData> OnCallRecordsList;
  31. public event Action<JsonData> OnAcceptInviteSuccess;
  32. public event Action<JsonData> OnRefuseInviteSuccess;
  33. private Dictionary<string,string> idList = new Dictionary<string, string>();
  34. public void GetSelfRoomList()
  35. {
  36. JsonData message = new JsonData();
  37. JsonData data = new JsonData();
  38. message["request"] = true;
  39. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  40. message["method"] = "selfRoomList";
  41. data["token"] = UserInfo.User_Token;
  42. message["data"] = data;
  43. WSHandler.SendMessage(message);
  44. CommonMethod.ShowNetLoading(WSHandler._client.name,"获取房间列表中...");
  45. idList.Add(message["id"].ToString(), message["method"].ToString());
  46. }
  47. public void SearchRoom(string searchContent)
  48. {
  49. JsonData message = new JsonData();
  50. JsonData data = new JsonData();
  51. message["request"] = true;
  52. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  53. message["method"] = "searchRoom";
  54. data["search"] = searchContent;
  55. data["token"] = UserInfo.User_Token;
  56. message["data"] = data;
  57. WSHandler.SendMessage(message);
  58. CommonMethod.ShowNetLoading(WSHandler._client.name, "查询房间中....");
  59. idList.Add(message["id"].ToString(), message["method"].ToString());
  60. }
  61. public void GetRoomID()
  62. {
  63. JsonData message = new JsonData();
  64. JsonData data = new JsonData();
  65. message["request"] = true;
  66. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  67. message["method"] = "getRoomId";
  68. WSHandler.SendMessage(message);
  69. CommonMethod.ShowNetLoading(WSHandler._client.name, "获取房间号...");
  70. idList.Add(message["id"].ToString(), message["method"].ToString());
  71. }
  72. public void GetRecordsList()
  73. {
  74. JsonData message = new JsonData();
  75. message["request"] = true;
  76. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  77. message["method"] = "callRecordsList";
  78. WSHandler.SendMessage(message);
  79. idList.Add(message["id"].ToString(), message["method"].ToString());
  80. }
  81. public void UserSearch(string search)
  82. {
  83. JsonData message = new JsonData();
  84. JsonData data = new JsonData();
  85. message["request"] = true;
  86. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  87. message["method"] = "userSearch";
  88. data["search"] = search;
  89. message["data"] = data;
  90. WSHandler.SendMessage(message);
  91. CommonMethod.ShowNetLoading(WSHandler._client.name, "搜索中...");
  92. idList.Add(message["id"].ToString(), message["method"].ToString());
  93. }
  94. //邀请用户
  95. public void InviteUsers(string roomId, List<InviteUserConfig> unionIds)
  96. {
  97. JsonData message = new JsonData();
  98. JsonData data = new JsonData();
  99. message["request"] = true;
  100. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  101. message["method"] = "inviteUsers";
  102. data["roomId"] = roomId;
  103. data["users"] = new JsonData();
  104. for (int i = 0; i < unionIds.Count; i++)
  105. {
  106. if (!string.IsNullOrEmpty(unionIds[i].UnionId))
  107. {
  108. JsonData user = new JsonData();
  109. user["unionId"] = unionIds[i].UnionId;
  110. data["users"].Add(user);
  111. }
  112. }
  113. message["data"] = data;
  114. WSHandler.SendMessage(message);
  115. CommonMethod.ShowNetLoading(WSHandler._client.name, "邀请中...");
  116. idList.Add(message["id"].ToString(), message["method"].ToString());
  117. }
  118. public void ChangeUserType(string type)
  119. {
  120. JsonData message = new JsonData();
  121. //JsonData data = new JsonData();
  122. message["notification"] = true;
  123. //message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  124. message["method"] = type;
  125. WSHandler.SendMessage(message);
  126. //idList.Add(message["id"].ToString(), message["method"].ToString());
  127. }
  128. //接受邀请
  129. public void AcceptInvite()
  130. {
  131. JsonData message = new JsonData();
  132. message["request"] = true;
  133. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  134. message["method"] = "invite:accept";
  135. WSHandler.SendMessage(message);
  136. CommonMethod.ShowNetLoading(WSHandler._client.name, "接受邀请...");
  137. idList.Add(message["id"].ToString(), message["method"].ToString());
  138. }
  139. //拒绝邀请
  140. public void RefuseInvite()
  141. {
  142. JsonData message = new JsonData();
  143. message["request"] = true;
  144. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  145. message["method"] = "invite:refuse";
  146. WSHandler.SendMessage(message);
  147. CommonMethod.ShowNetLoading(WSHandler._client.name, "拒绝邀请...");
  148. idList.Add(message["id"].ToString(), message["method"].ToString());
  149. }
  150. public void JoinRoom(string roomId)
  151. {
  152. JsonData message = new JsonData();
  153. JsonData data = new JsonData();
  154. message["request"] = true;
  155. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  156. message["method"] = "joinRoom";
  157. message["data"] = new JsonData();
  158. message["data"]["roomId"] = roomId;
  159. WSHandler.SendMessage(message);
  160. CommonMethod.ShowNetLoading(WSHandler._client.name, "检测房间号...");
  161. idList.Add(message["id"].ToString(), message["method"].ToString());
  162. }
  163. public void OnMessageReceived(JsonData data)
  164. {
  165. if (data.Keys.Contains("response"))
  166. {
  167. CommonMethod.HideNetLoading(WSHandler._client.name);
  168. //Debug.Log(data["data"].ToString());
  169. //Debug.Log(data["data"]["roomId"].ToString());
  170. // string roomid = data["data"]["roomId"].ToString();
  171. // AgoraVideoAudioManager.Instance.uid =(uint)int.Parse(data["id"].ToString());
  172. // AgoraVideoAudioManager.Instance.SetChinnelName(roomid);
  173. if (data["data"].Keys.Contains("roomId"))
  174. {
  175. // 设置频道号
  176. // AgoraVideoAudioManager.Instance.SetChinnelName(data["data"]["roomId"].ToString());
  177. }
  178. }
  179. if (data.Keys.Contains("errorCode"))
  180. {
  181. CommonMethod.HideLoading();
  182. CommonMethod.ShowTextTusi();
  183. return;
  184. }
  185. if (data.Keys.Contains("method"))
  186. {
  187. switch (data["method"].ToString())
  188. {
  189. case "connect":
  190. InitReceived(data);
  191. break;
  192. case "roomChange":
  193. //OnRoomChangeReveived(data);
  194. break;
  195. case "repeat":
  196. ShowLogin.Instance.loginType = LoginType.INIT;
  197. ScenesManager.Instance.showWindow(SceneType.ShowLogin);
  198. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.PopOk, RtcStrConfig.otherLogin,"返回登录");
  199. WSHandler.roomRtcCloes();
  200. WSHandler.clientClosed();
  201. if (ShowRoom.Instance)
  202. {
  203. ShowRoom.Instance.ExitRoom();
  204. }
  205. break;
  206. case "invite:refuse":
  207. OnRefuse?.Invoke(data);
  208. break;
  209. case "invite:timeout":
  210. OnTimeout?.Invoke(data);
  211. break;
  212. case "invitee":
  213. OnInvitee?.Invoke(data);
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219. if (data.Keys.Contains("id"))
  220. {
  221. if (idList.ContainsKey(data["id"].ToString()))
  222. {
  223. switch (idList[data["id"].ToString()])
  224. {
  225. case "selfRoomList":
  226. GetSelfRoomListReveived(data);
  227. break;
  228. case "searchRoom":
  229. OnSearchRoomReveived(data);
  230. break;
  231. case "getRoomId":
  232. OnGetRoomIdReveived(data);
  233. break;
  234. case "joinRoom":
  235. OnJoinRoomReveived(data);
  236. break;
  237. case "userSearch":
  238. OnUserSearch?.Invoke(data);
  239. break;
  240. case "inviteUsers":
  241. OnInviteUsers?.Invoke(data);
  242. break;
  243. case "callRecordsList":
  244. OnCallRecordsList?.Invoke(data);
  245. break;
  246. case "invite:refuse":
  247. OnRefuseInviteSuccess?.Invoke(data);
  248. break;
  249. case "invite:accept":
  250. OnAcceptInviteSuccess?.Invoke(data);
  251. break;
  252. default:
  253. break;
  254. }
  255. idList.Remove(data["id"].ToString());
  256. }
  257. }
  258. }
  259. private void InitReceived(JsonData data)
  260. {
  261. if (OnInit != null)
  262. {
  263. OnInit(data);
  264. }
  265. }
  266. public void GetSelfRoomListReveived(JsonData data)
  267. {
  268. if (OnGetSelfRoomList != null)
  269. {
  270. OnGetSelfRoomList(data);
  271. }
  272. }
  273. public void OnSearchRoomReveived(JsonData data)
  274. {
  275. if (OnSearchRoom != null)
  276. {
  277. OnSearchRoom(data);
  278. }
  279. }
  280. public void OnRoomChangeReveived(JsonData data)
  281. {
  282. if (OnRoomChange != null)
  283. {
  284. OnRoomChange(data);
  285. }
  286. }
  287. }