MQTTManager.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. public class MQTTManager : MonoSingleton<MQTTManager>,IController
  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 account;
  18. public string roomId;
  19. public string email;
  20. public string phone;
  21. public string roleId;
  22. public string resourcePool;
  23. public int resourcePoolId;
  24. public string expertType;
  25. public string _username;
  26. public string room;
  27. public static string certification;
  28. /// <summary>
  29. /// 获取信令
  30. /// </summary>
  31. public void Signal()
  32. {
  33. GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.signal_certification, "", SignalCallBack));
  34. // GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.userinfo, "", UserInfoCallBack));
  35. // HttpTool.Instance.PostMqtt(HttpAction.signal_certification, "", SignalCallBack);
  36. }
  37. public void SetAccoutInfo(string account)
  38. {
  39. MQTTManager.Instance.account = account;
  40. }
  41. private void SignalCallBack(string msg)
  42. {
  43. Debug.Log(msg);
  44. JObject obj = JObject.Parse(msg);
  45. if (obj["code"].ToString() == "200")
  46. {
  47. HttpActionLang.Instance.mqttSocket = obj["data"]["host"].ToString();
  48. MQTTManager.certification = obj["data"]["certification"].ToString();
  49. MQTTManager.Instance.front = obj["data"]["topicPrefix"].ToString();
  50. MQTTManager.Instance.id = obj["data"]["clientId"].ToString();
  51. MQTTManager.Instance.username = obj["data"]["username"].ToString();
  52. MQTTClient.Instance.Connect();
  53. // StartCoroutine(TestCreateRoom());
  54. }
  55. else
  56. {
  57. Debug.LogError("MQTT 获取信令失败 !!!" + obj["code"].ToString());
  58. }
  59. }
  60. private IEnumerator TestCreateRoom()
  61. {
  62. yield return new WaitForSeconds(5);
  63. while (!RTCRoomManager.Instance.isRTCreate)
  64. {
  65. yield return new WaitForSeconds(1);
  66. this.GetService<IRTCService>().CreatRoom();
  67. }
  68. }
  69. private void UserInfoCallBack(string msg)
  70. {
  71. Debug.Log(msg);
  72. JObject obj = JObject.Parse(msg);
  73. if (obj["code"].ToString() == "200")
  74. {
  75. MQTTManager.Instance._username = obj["data"]["name"].ToString();
  76. MQTTManager.Instance.roleId = obj["data"]["roleId"].ToString();
  77. MQTTManager.Instance.email = obj["data"]["email"].ToString();
  78. MQTTManager.Instance.phone = obj["data"]["phone"].ToString();
  79. MQTTManager.Instance.resourcePool = obj["data"]["resourcePool"].ToString();
  80. MQTTManager.Instance.expertType = obj["data"]["expertType"].ToString();
  81. }
  82. else
  83. {
  84. Debug.LogError("获取用户信息失败 !!!"+ obj["code"].ToString());
  85. }
  86. }
  87. public void SetRoomId(string id)
  88. {
  89. MQTTManager.Instance.roomId = id;
  90. // MQTTClient.Instance.roomId = "9527";
  91. // 订阅房间频道
  92. MQTTClient.Instance.Subscribe(MQTTManager.Instance.front + "room/" + id);
  93. }
  94. public void UnSubscribe( string id)
  95. {
  96. MQTTClient.Instance.UnSubscribe(MQTTManager.Instance.front + "room/" + id);
  97. }
  98. /// <summary>
  99. /// 发送消息
  100. /// </summary>
  101. /// <param name="msg"></param>
  102. public void PushMsg(string msg)
  103. {
  104. if (!string.IsNullOrEmpty(msg))
  105. {
  106. JsonData json = new JsonData();
  107. json["account"] = MQTTManager.Instance.account;
  108. json["Sid"] = "sid";//选择的场景id
  109. json["msg"] = msg;
  110. MQTTClient.Instance.publish(Encoding.UTF8.GetBytes(json.ToJson()));
  111. }
  112. }
  113. /// <summary>
  114. /// 发送消息
  115. /// </summary>
  116. /// <param name="msg"></param>
  117. public void PushMsg(string channelID, string msg)
  118. {
  119. if (!string.IsNullOrEmpty(msg))
  120. {
  121. MQTTClient.Instance.publish(channelID, Encoding.UTF8.GetBytes(msg));
  122. }
  123. }
  124. /// <summary>
  125. /// 接受消息
  126. /// </summary>
  127. /// <param name="msg"></param>
  128. public void Received(string msg)
  129. {
  130. JObject obj = JObject.Parse(msg);
  131. Debug.Log("Received ===> " + obj["action"].ToString());
  132. switch (obj["action"].ToString())
  133. {
  134. case "timeout": // 超时
  135. UICallManager.Instance.EventTimeOut();
  136. break;
  137. case "invite": // 邀请
  138. if(obj["control"] !=null)
  139. {
  140. string phone = obj["control"]["phone"].ToString();
  141. RTCRoomManager.Instance.controlPhone = phone;
  142. if(RTCRoomManager.Instance.rtcState != RTCEnum.OFF) // 当前已经在房间内 直接发送当前房间号
  143. {
  144. JsonData data = new JsonData();
  145. data["action"] = "invite";
  146. data["rtcRoom"] =RTCRoomManager.Instance.mRTCRoomInfo.roomId.ToString();
  147. data["personnel"] = new JsonData();
  148. data["personnel"]["id"] = MQTTManager.Instance.id;
  149. data["personnel"]["name"] = MQTTManager.Instance._username;
  150. data["personnel"]["resourcePoo1"] = MQTTManager.Instance.resourcePool;
  151. data["personnel"]["room"] = MQTTManager.Instance.room;
  152. data["personnel"]["phone"] = MQTTManager.Instance.phone;
  153. // 用MQTT 向中考发送邀请
  154. MQTTManager.Instance.PushMsg(MQTTManager.Instance.front + "online/" + phone, data.ToJson());
  155. }
  156. else // 不在房间内,创建房间后 发送邀请
  157. {
  158. //string roomid = obj["rtcRoom"].ToString();
  159. //string resourcePool = obj["control"]["resourcePoo1"].ToString();
  160. RTCRoomManager.Instance.isInvite = true;
  161. UICallManager.Instance.EventInvite(obj["control"].ToString(), "");
  162. Debug.Log(phone);
  163. }
  164. }
  165. else
  166. {
  167. Debug.LogError(" 非控制台邀请 ");
  168. }
  169. break;
  170. case "noExpert": // 无专家
  171. UICallManager.Instance.EventNoExpert();
  172. break;
  173. case "chat": // 聊天消息
  174. // 拆解用户名 和 聊天内容 显示到聊天框里
  175. string username = obj["from"].ToString();
  176. string chatMsg = obj["chatMessage"].ToString();
  177. FileList.Instance.AddMsgItem(null, username, chatMsg);
  178. break;
  179. case "share": // 接受文件
  180. // 拆解用户名 和 文件名称 显示到聊天框里 下载文件
  181. FileConfig fileConfig = JsonConvert.DeserializeObject<FileConfig>(obj["chatMessage"].ToString());
  182. fileConfig.From = obj["from"].ToString();
  183. Debug.Log(" DGJ ===> FileConfig " + JsonConvert.SerializeObject(fileConfig));
  184. FileList.Instance.AddItem(fileConfig);
  185. break;
  186. case "remark": // 空间标注
  187. float Valuex = float.Parse(obj["chatMessage"]["x"].ToString());
  188. float Valuey = float.Parse(obj["chatMessage"]["y"].ToString());
  189. ArrowList.Instance.AddArrow(Valuex, Valuey);
  190. break;
  191. case "clearRemark":
  192. ArrowList.Instance.DeleteAll();
  193. break;
  194. default:
  195. break;
  196. }
  197. }
  198. }