MQTTManager.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. public class MQTTManager : MonoSingleton<MQTTManager>
  10. {
  11. public string front = "client/";
  12. public string id = "";
  13. public string username = "u@unity3";// 需要根据服务器设置
  14. public string password = null;// 需要根据服务器设置
  15. public string rid = "mqttx_b4c02ddc"; //其他人的ID
  16. public string account;
  17. public string roomId;
  18. public string email;
  19. public string phone;
  20. public string roleId;
  21. public string resourcePool;
  22. public string expertType;
  23. public string _username;
  24. public string room;
  25. public static string certification;
  26. /// <summary>
  27. /// 获取信令
  28. /// </summary>
  29. public void Signal()
  30. {
  31. GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.signal_certification, "", SignalCallBack));
  32. // GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.userinfo, "", UserInfoCallBack));
  33. // HttpTool.Instance.PostMqtt(HttpAction.signal_certification, "", SignalCallBack);
  34. }
  35. public void SetAccoutInfo(string account)
  36. {
  37. MQTTManager.Instance.account = account;
  38. }
  39. private void SignalCallBack(string msg)
  40. {
  41. Debug.Log(msg);
  42. JObject obj = JObject.Parse(msg);
  43. if (obj["code"].ToString() == "200")
  44. {
  45. HttpActionLang.Instance.mqttSocket = obj["data"]["host"].ToString();
  46. MQTTManager.certification = obj["data"]["certification"].ToString();
  47. MQTTManager.Instance.front = obj["data"]["topicPrefix"].ToString();
  48. MQTTManager.Instance.id = obj["data"]["clientId"].ToString();
  49. MQTTManager.Instance.username = obj["data"]["username"].ToString();
  50. MQTTClient.Instance.Connect();
  51. }
  52. else
  53. {
  54. Debug.LogError("MQTT 获取信令失败 !!!" + obj["code"].ToString());
  55. }
  56. }
  57. private void UserInfoCallBack(string msg)
  58. {
  59. Debug.Log(msg);
  60. JObject obj = JObject.Parse(msg);
  61. if (obj["code"].ToString() == "200")
  62. {
  63. MQTTManager.Instance._username = obj["data"]["name"].ToString();
  64. MQTTManager.Instance.roleId = obj["data"]["roleId"].ToString();
  65. MQTTManager.Instance.email = obj["data"]["email"].ToString();
  66. MQTTManager.Instance.phone = obj["data"]["phone"].ToString();
  67. MQTTManager.Instance.resourcePool = obj["data"]["resourcePool"].ToString();
  68. MQTTManager.Instance.expertType = obj["data"]["expertType"].ToString();
  69. }
  70. else
  71. {
  72. Debug.LogError("获取用户信息失败 !!!"+ obj["code"].ToString());
  73. }
  74. }
  75. public void SetRoomId(string id)
  76. {
  77. MQTTManager.Instance.roomId = id;
  78. // MQTTClient.Instance.roomId = "9527";
  79. // 订阅房间频道
  80. MQTTClient.Instance.Subscribe(MQTTManager.Instance.front + "room/" + id);
  81. }
  82. /// <summary>
  83. /// 发送消息
  84. /// </summary>
  85. /// <param name="msg"></param>
  86. public void PushMsg(string msg)
  87. {
  88. if (!string.IsNullOrEmpty(msg))
  89. {
  90. JsonData json = new JsonData();
  91. json["account"] = MQTTManager.Instance.account;
  92. json["Sid"] = "sid";//选择的场景id
  93. json["msg"] = msg;
  94. MQTTClient.Instance.publish(Encoding.UTF8.GetBytes(json.ToJson()));
  95. }
  96. }
  97. /// <summary>
  98. /// 发送消息
  99. /// </summary>
  100. /// <param name="msg"></param>
  101. public void PushMsg(string channelID, string msg)
  102. {
  103. if (!string.IsNullOrEmpty(msg))
  104. {
  105. MQTTClient.Instance.publish(channelID, Encoding.UTF8.GetBytes(msg));
  106. }
  107. }
  108. /// <summary>
  109. /// 接受消息
  110. /// </summary>
  111. /// <param name="msg"></param>
  112. public void Received(string msg)
  113. {
  114. JObject obj = JObject.Parse(msg);
  115. Debug.Log("Received ===> " + obj["action"].ToString());
  116. switch (obj["action"].ToString())
  117. {
  118. case "timeout": // 超时
  119. UICallManager.Instance.EventTimeOut();
  120. break;
  121. case "invite": // 邀请
  122. if(obj["control"] !=null)
  123. {
  124. //string roomid = obj["rtcRoom"].ToString();
  125. string phone = obj["control"]["phone"].ToString();
  126. //string resourcePool = obj["control"]["resourcePoo1"].ToString();
  127. UICallManager.Instance.EventInvite(obj["control"].ToString(),"");
  128. Debug.Log(phone);
  129. RTCRoomManager.Instance.controlPhone = phone;
  130. }
  131. else
  132. {
  133. Debug.LogError(" 非控制台邀请 ");
  134. }
  135. break;
  136. case "noExpert": // 无专家
  137. UICallManager.Instance.EventNoExpert();
  138. break;
  139. case "chat": // 聊天消息
  140. // 拆解用户名 和 聊天内容 显示到聊天框里
  141. string username = obj["from"].ToString();
  142. string chatMsg = obj["chatMessage"].ToString();
  143. FileList.Instance.AddMsgItem(null, username, chatMsg);
  144. break;
  145. case "share": // 接受文件
  146. // 拆解用户名 和 文件名称 显示到聊天框里 下载文件
  147. FileConfig fileConfig = JsonConvert.DeserializeObject<FileConfig>(obj["chatMessage"].ToString());
  148. fileConfig.From = obj["from"].ToString();
  149. Debug.Log(" DGJ ===> FileConfig " + JsonConvert.SerializeObject(fileConfig));
  150. FileList.Instance.AddItem(fileConfig);
  151. break;
  152. case "remark": // 空间标注
  153. float Valuex = float.Parse(obj["chatMessage"]["x"].ToString());
  154. float Valuey = float.Parse(obj["chatMessage"]["y"].ToString());
  155. ArrowList.Instance.AddArrow(Valuex, Valuey);
  156. break;
  157. case "clearRemark":
  158. ArrowList.Instance.DeleteAll();
  159. break;
  160. default:
  161. break;
  162. }
  163. }
  164. }