MQTTManager.cs 6.2 KB

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