MQTTManager.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. using LitJson;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using UnityEngine;
  8. using static LangChaoRommMinIo;
  9. using uPLibrary.Networking.M2Mqtt.Messages;
  10. public class MQTTManager : MonoSingleton<MQTTManager>
  11. {
  12. public string front = "client/";
  13. public string id = "";
  14. public string username = "u@unity3";// 需要根据服务器设置
  15. public string password = null;// 需要根据服务器设置
  16. public string rid = "mqttx_b4c02ddc"; //其他人的ID
  17. public string clientId = "";
  18. public string account;
  19. public string roomId;
  20. public string email;
  21. public string phone;
  22. public string roleId;
  23. // public string resourcePool;
  24. public string resourcePoolId;
  25. public string expertType;
  26. public string _username;
  27. public string room;
  28. public static string certification;
  29. public string topiceNavigation;
  30. public string topiceSync;
  31. public string topiceClose;
  32. public bool isCreateRoom = false;
  33. bool isinitCRoom = true;
  34. private int SignalCount = 0;
  35. private void Update()
  36. {
  37. if(isCreateRoom&& isinitCRoom)
  38. {
  39. isinitCRoom = false;
  40. StartCoroutine(CreateRoom());
  41. }
  42. }
  43. private IEnumerator CreateRoom()
  44. {
  45. yield return new WaitForSeconds(1);
  46. // UICallManager.Instance.EventInvite("", "");
  47. }
  48. /// <summary>
  49. /// 获取信令
  50. /// </summary>
  51. public void Signal()
  52. {
  53. HttpToolLangChao.Instance.Post(HttpActionLang.signal_certification, "", SignalCallBack);
  54. SignalCount++;
  55. // GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.userinfo, "", UserInfoCallBack));
  56. // HttpTool.Instance.PostMqtt(HttpAction.signal_certification, "", SignalCallBack);
  57. }
  58. public void SetAccoutInfo(string account)
  59. {
  60. MQTTManager.Instance.account = account;
  61. }
  62. private void SignalCallBack(string msg)
  63. {
  64. Debug.Log("SignalCallBack " + msg);
  65. JObject obj = JObject.Parse(msg);
  66. if (obj["code"].ToString() == "200")
  67. {
  68. HttpActionLang.Instance.mqttSocket = obj["data"]["host"].ToString();
  69. // HttpActionLang.Instance.mqttPort = obj["data"]["port"].ToString();
  70. MQTTManager.certification = obj["data"]["certification"].ToString();
  71. MQTTManager.Instance.front = obj["data"]["topicPrefix"].ToString();
  72. MQTTManager.Instance.clientId = obj["data"]["clientId"].ToString();
  73. MQTTManager.Instance.username = obj["data"]["username"].ToString();
  74. //if (!string.IsNullOrEmpty( obj["data"]["uinoTopics"].ToString()))
  75. //{
  76. // topiceNavigation = obj["data"]["uinoTopics"]["navigation"].ToString();
  77. // topiceSync = obj["data"]["uinoTopics"]["sync"].ToString();
  78. // topiceClose = obj["data"]["uinoTopics"]["close"].ToString();
  79. //}
  80. // ErrorPopup.Instance.UpdateEmqxState(NetState.Connecting);
  81. MQTTClient.Instance.Connect();
  82. SignalCount = 0;
  83. // StartCoroutine(TestCreateRoom());
  84. }
  85. else
  86. {
  87. Debug.LogError("MQTT 获取信令失败 !!!" + obj["code"].ToString());
  88. if(int.Parse(obj["code"].ToString())>= 500)
  89. {
  90. //ErrorPopup.Instance.UpdateEmqxState(NetState.DisConnect);
  91. //ErrorPopup.Instance.ShowEmqxError("服务器连接失败 "+ msg, -1);
  92. }
  93. else
  94. {
  95. // ErrorPopup.Instance.UpdateEmqxState(NetState.StartError);
  96. if (SignalCount > 5)
  97. {
  98. // ErrorPopup.Instance.ShowEmqxError("信令请求失败 " + msg , -1);
  99. }
  100. else
  101. {
  102. Signal();
  103. }
  104. }
  105. }
  106. }
  107. private IEnumerator TestCreateRoom()
  108. {
  109. yield return new WaitForSeconds(5);
  110. while (!RTCRoomManager.Instance.isRTCreate)
  111. {
  112. yield return new WaitForSeconds(1);
  113. // this.GetService<IRTCService>().CreatRoom();
  114. }
  115. }
  116. private void UserInfoCallBack(string msg)
  117. {
  118. Debug.Log(msg);
  119. JObject obj = JObject.Parse(msg);
  120. if (obj["code"].ToString() == "200")
  121. {
  122. MQTTManager.Instance._username = obj["data"]["name"].ToString();
  123. MQTTManager.Instance.roleId = obj["data"]["roleId"].ToString();
  124. MQTTManager.Instance.email = obj["data"]["email"].ToString();
  125. MQTTManager.Instance.phone = obj["data"]["phone"].ToString();
  126. // MQTTManager.Instance.resourcePool = obj["data"]["resourcePool"].ToString();
  127. MQTTManager.Instance.resourcePoolId = obj["data"]["resourcePoolId"].ToString();
  128. MQTTManager.Instance.expertType = obj["data"]["expertType"].ToString();
  129. }
  130. else
  131. {
  132. Debug.LogError("获取用户信息失败 !!!"+ obj["code"].ToString());
  133. }
  134. }
  135. public void SetRoomId(string id)
  136. {
  137. MQTTManager.Instance.roomId = id;
  138. // MQTTClient.Instance.roomId = "9527";
  139. // 订阅房间频道
  140. MQTTClient.Instance.Subscribe(MQTTManager.Instance.front + "room/" + id);
  141. }
  142. public void UnSubscribe( string id)
  143. {
  144. MQTTClient.Instance.UnSubscribe(MQTTManager.Instance.front + "room/" + id);
  145. }
  146. /// <summary>
  147. /// 发送消息
  148. /// </summary>
  149. /// <param name="msg"></param>
  150. public void PushMsg(string msg)
  151. {
  152. if (!string.IsNullOrEmpty(msg))
  153. {
  154. JsonData json = new JsonData();
  155. json["account"] = MQTTManager.Instance.account;
  156. json["Sid"] = "sid";//选择的场景id
  157. json["msg"] = msg;
  158. MQTTClient.Instance.publish(Encoding.UTF8.GetBytes(json.ToJson()));
  159. }
  160. }
  161. /// <summary>
  162. /// 发送消息
  163. /// </summary>
  164. /// <param name="msg"></param>
  165. public void PushMsg(string channelID, string msg)
  166. {
  167. Debug.Log("DGJ ===> MQTT Test "+ msg);
  168. if (!string.IsNullOrEmpty(msg))
  169. {
  170. MQTTClient.Instance.publish(channelID, Encoding.UTF8.GetBytes(msg));
  171. }
  172. }
  173. /// <summary>
  174. /// 接受消息
  175. /// </summary>
  176. /// <param name="msg"></param>
  177. public void Received(string msg)
  178. {
  179. JObject obj = JObject.Parse(msg);
  180. // Debug.Log("Received ===> " + obj["action"].ToString());
  181. if(obj["action"]!=null)
  182. switch (obj["action"].ToString())
  183. {
  184. case "timeout": // 超时
  185. // UICallManager.Instance.EventTimeOut();
  186. break;
  187. case "invite": // 邀请
  188. if(obj["control"] !=null)
  189. {
  190. string id = obj["control"]["id"].ToString();
  191. RTCRoomManager.Instance.controlId = id;
  192. if(RTCRoomManager.Instance.rtcState != RTCEnum.OFF) // 当前已经在房间内 直接发送当前房间号
  193. {
  194. JsonData data = new JsonData();
  195. data["action"] = "invite";
  196. data["rtcRoom"] =RTCRoomManager.Instance.mRTCRoomInfo.roomId.ToString();
  197. data["personnel"] = new JsonData();
  198. data["personnel"]["id"] = MQTTManager.Instance.id;
  199. data["personnel"]["name"] = MQTTManager.Instance._username;
  200. //data["personnel"]["resourcePoo1"] = MQTTManager.Instance.resourcePool;
  201. data["personnel"]["resourcePoolId"] = MQTTManager.Instance.resourcePoolId;
  202. data["personnel"]["room"] = MQTTManager.Instance.room;
  203. data["personnel"]["phone"] = MQTTManager.Instance.phone;
  204. // 用MQTT 向中考发送邀请
  205. MQTTManager.Instance.PushMsg(MQTTManager.Instance.front + "online/" + RTCRoomManager.Instance.controlId, data.ToJson());
  206. }
  207. else // 不在房间内,创建房间后 发送邀请
  208. {
  209. //string roomid = obj["rtcRoom"].ToString();
  210. //string resourcePool = obj["control"]["resourcePoo1"].ToString();
  211. RTCRoomManager.Instance.isInvite = true;
  212. // UICallManager.Instance.EventInvite(obj["control"].ToString(), "");
  213. Debug.Log(phone);
  214. }
  215. }
  216. else
  217. {
  218. Debug.LogError(" 非控制台邀请 ");
  219. }
  220. break;
  221. case "noExpert": // 无专家
  222. // UICallManager.Instance.EventNoExpert();
  223. break;
  224. case "chat": // 聊天消息
  225. // 拆解用户名 和 聊天内容 显示到聊天框里
  226. string username = obj["from"].ToString();
  227. string chatMsg = obj["chatMessage"].ToString();
  228. FileList.Instance.AddMsgItem(null, username, chatMsg);
  229. break;
  230. case "share": // 接受文件
  231. Debug.Log(" DGJ ===> share " + obj["chatMessage"].ToString());
  232. // 拆解用户名 和 文件名称 显示到聊天框里 下载文件
  233. FileConfig fileConfig = JsonConvert.DeserializeObject<FileConfig>(obj["chatMessage"].ToString());
  234. fileConfig.From = obj["from"].ToString();
  235. Debug.Log(" DGJ ===> FileConfig " + JsonConvert.SerializeObject(fileConfig));
  236. FileList.Instance.AddItem(fileConfig);
  237. break;
  238. case "remark": // 空间标注
  239. float Valuex = float.Parse(obj["chatMessage"]["x"].ToString());
  240. float Valuey = float.Parse(obj["chatMessage"]["y"].ToString());
  241. ArrowList.Instance.AddArrow(Valuex, Valuey);
  242. break;
  243. case "clearRemark":
  244. ArrowList.Instance.DeleteAll();
  245. break;
  246. case "leave":
  247. // 退出房间
  248. string roleId = obj["roleId"].ToString();
  249. if (roleId == "2")
  250. {
  251. // RTCRoomManager.Instance.UserOffline(UserEnum.Expert);
  252. }
  253. if (roleId == "1")
  254. {
  255. // RTCRoomManager.Instance.UserOffline(UserEnum.Conterl);
  256. }
  257. break;
  258. case "stop":
  259. // 停止同步位置信息
  260. SynPlayerTransform.Instance.isPushPos = false;
  261. break;
  262. default:
  263. break;
  264. }
  265. if (obj["image_data"]!=null&&!string.IsNullOrEmpty( obj["image_data"].ToString()))
  266. {
  267. // UILogManager.Instance.text2.text = msg;
  268. SynPlayerTransform.Instance.isPushPos = true;
  269. // LangChaoMailManager.Instance.ReceiverMail(obj);
  270. }
  271. }
  272. public void Received(MqttMsgPublishEventArgs MsgData)
  273. {
  274. string msg = Encoding.UTF8.GetString(MsgData.Message);
  275. JObject obj = JObject.Parse(msg);
  276. // Debug.Log("Received ===> " + obj["action"].ToString());
  277. if (obj["action"] != null)
  278. switch (obj["action"].ToString())
  279. {
  280. case "timeout": // 超时
  281. // UICallManager.Instance.EventTimeOut();
  282. break;
  283. case "invite": // 邀请
  284. if (obj["control"] != null)
  285. {
  286. string id = obj["control"]["id"].ToString();
  287. RTCRoomManager.Instance.controlId = id;
  288. if (RTCRoomManager.Instance.rtcState != RTCEnum.OFF) // 当前已经在房间内 直接发送当前房间号
  289. {
  290. JsonData data = new JsonData();
  291. data["action"] = "invite";
  292. data["rtcRoom"] = RTCRoomManager.Instance.mRTCRoomInfo.roomId.ToString();
  293. data["personnel"] = new JsonData();
  294. data["personnel"]["id"] = MQTTManager.Instance.id;
  295. data["personnel"]["name"] = MQTTManager.Instance._username;
  296. //data["personnel"]["resourcePoo1"] = MQTTManager.Instance.resourcePool;
  297. data["personnel"]["resourcePoolId"] = MQTTManager.Instance.resourcePoolId;
  298. data["personnel"]["room"] = MQTTManager.Instance.room;
  299. data["personnel"]["phone"] = MQTTManager.Instance.phone;
  300. // 用MQTT 向中考发送邀请
  301. MQTTManager.Instance.PushMsg(MQTTManager.Instance.front + "online/" + RTCRoomManager.Instance.controlId, data.ToJson());
  302. }
  303. else // 不在房间内,创建房间后 发送邀请
  304. {
  305. //string roomid = obj["rtcRoom"].ToString();
  306. //string resourcePool = obj["control"]["resourcePoo1"].ToString();
  307. RTCRoomManager.Instance.isInvite = true;
  308. // UICallManager.Instance.EventInvite(obj["control"].ToString(), "");
  309. Debug.Log(phone);
  310. }
  311. }
  312. else
  313. {
  314. Debug.LogError(" 非控制台邀请 ");
  315. }
  316. break;
  317. case "noExpert": // 无专家
  318. // UICallManager.Instance.EventNoExpert();
  319. break;
  320. case "chat": // 聊天消息
  321. // 拆解用户名 和 聊天内容 显示到聊天框里
  322. string username = obj["from"].ToString();
  323. string chatMsg = obj["chatMessage"].ToString();
  324. FileList.Instance.AddMsgItem(null, username, chatMsg);
  325. break;
  326. case "share": // 接受文件
  327. Debug.Log(" DGJ ===> share " + obj["chatMessage"].ToString());
  328. // 拆解用户名 和 文件名称 显示到聊天框里 下载文件
  329. FileConfig fileConfig = JsonConvert.DeserializeObject<FileConfig>(obj["chatMessage"].ToString());
  330. fileConfig.From = obj["from"].ToString();
  331. Debug.Log(" DGJ ===> FileConfig " + JsonConvert.SerializeObject(fileConfig));
  332. FileList.Instance.AddItem(fileConfig);
  333. break;
  334. case "remark": // 空间标注
  335. float Valuex = float.Parse(obj["chatMessage"]["x"].ToString());
  336. float Valuey = float.Parse(obj["chatMessage"]["y"].ToString());
  337. ArrowList.Instance.AddArrow(Valuex, Valuey);
  338. break;
  339. case "clearRemark":
  340. ArrowList.Instance.DeleteAll();
  341. break;
  342. case "leave":
  343. // 退出房间
  344. string roleId = obj["roleId"].ToString();
  345. if (roleId == "2")
  346. {
  347. // RTCRoomManager.Instance.UserOffline(UserEnum.Expert);
  348. }
  349. if (roleId == "1")
  350. {
  351. // RTCRoomManager.Instance.UserOffline(UserEnum.Conterl);
  352. }
  353. break;
  354. default:
  355. break;
  356. }
  357. switch (MsgData.Topic)
  358. {
  359. case "yunyi/coordinate/close":
  360. if (obj["action"] != null&& (obj["action"].ToString()== "stop"))
  361. {
  362. // 停止同步位置信息
  363. SynPlayerTransform.Instance.isPushPos = false;
  364. // MailUIContent.Instance.CloseSync.SetActive(false);
  365. }
  366. break;
  367. case "yunyi/navigation/diagram":
  368. if (obj["image_data"] != null && !string.IsNullOrEmpty(obj["image_data"].ToString()))
  369. {
  370. // UILogManager.Instance.text2.text = msg;
  371. SynPlayerTransform.Instance.isPushPos = true;
  372. //MailUIContent.Instance.CloseSync.SetActive(true);
  373. //LangChaoMailManager.Instance.ReceiverMail(obj);
  374. }
  375. break;
  376. default:
  377. break;
  378. }
  379. }
  380. }