MQTTManager.cs 16 KB

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