MQTTManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 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 bool isCreateRoom = false;
  30. bool isinitCRoom = true;
  31. private void Update()
  32. {
  33. if (isCreateRoom && isinitCRoom)
  34. {
  35. isinitCRoom = false;
  36. StartCoroutine(CreateRoom());
  37. }
  38. }
  39. private IEnumerator CreateRoom()
  40. {
  41. yield return new WaitForSeconds(5);
  42. Debug.Log(" 创建房间 ");
  43. UICallManager.Instance.EventInvite("", "");
  44. //CoroutineSystem.Instance.StartCoroutine(
  45. // HttpTool.Instance.SendHttp(HttpActionLang.rtc_CreateRoom, "", message =>
  46. // {
  47. // JObject jobject = JObject.Parse(message);
  48. // Debug.Log("DGJ ===> CreateRoom " + message);
  49. // // "Request Timeout"
  50. // if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
  51. // {
  52. // // this.SendEvent(new RTCCreatRoomSuccessEvent() { rtcRoomInfo = jobject["data"].ToString() });
  53. // RTCRoomInfo mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(jobject["data"].ToString());
  54. // UICallManager.Instance.SetMQTTRoomId(mRTCRoomInfo.roomId.ToString());
  55. // }
  56. // else if (jobject["code"].ToString() == "503")
  57. // {
  58. // RTCRoomManager.Instance.isRTCreate = true;
  59. // }
  60. // }, "application/x-www-form-urlencoded"));
  61. }
  62. /// <summary>
  63. /// 获取信令
  64. /// </summary>
  65. public void Signal()
  66. {
  67. GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.signal_certification, "", SignalCallBack));
  68. // GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.userinfo, "", UserInfoCallBack));
  69. // HttpTool.Instance.PostMqtt(HttpAction.signal_certification, "", SignalCallBack);
  70. }
  71. public void SetAccoutInfo(string account)
  72. {
  73. MQTTManager.Instance.account = account;
  74. }
  75. private void SignalCallBack(string msg)
  76. {
  77. Debug.Log(msg);
  78. JObject obj = JObject.Parse(msg);
  79. if (obj["code"].ToString() == "200")
  80. {
  81. HttpActionLang.Instance.mqttSocket = obj["data"]["host"].ToString();
  82. MQTTManager.certification = obj["data"]["certification"].ToString();
  83. MQTTManager.Instance.front = obj["data"]["topicPrefix"].ToString();
  84. MQTTManager.Instance.clientId = obj["data"]["clientId"].ToString();
  85. MQTTManager.Instance.username = obj["data"]["username"].ToString();
  86. MQTTClient.Instance.Connect();
  87. // StartCoroutine(TestCreateRoom());
  88. }
  89. else
  90. {
  91. Debug.LogError("MQTT 获取信令失败 !!!" + obj["code"].ToString());
  92. }
  93. }
  94. private IEnumerator TestCreateRoom()
  95. {
  96. yield return new WaitForSeconds(5);
  97. while (!RTCRoomManager.Instance.isRTCreate)
  98. {
  99. yield return new WaitForSeconds(1);
  100. this.GetService<IRTCService>().CreatRoom();
  101. }
  102. }
  103. private void UserInfoCallBack(string msg)
  104. {
  105. Debug.Log(msg);
  106. JObject obj = JObject.Parse(msg);
  107. if (obj["code"].ToString() == "200")
  108. {
  109. MQTTManager.Instance._username = obj["data"]["name"].ToString();
  110. MQTTManager.Instance.roleId = obj["data"]["roleId"].ToString();
  111. MQTTManager.Instance.email = obj["data"]["email"].ToString();
  112. MQTTManager.Instance.phone = obj["data"]["phone"].ToString();
  113. // MQTTManager.Instance.resourcePool = obj["data"]["resourcePool"].ToString();
  114. MQTTManager.Instance.resourcePoolId = obj["data"]["resourcePoolId"].ToString();
  115. MQTTManager.Instance.expertType = obj["data"]["expertType"].ToString();
  116. }
  117. else
  118. {
  119. Debug.LogError("获取用户信息失败 !!!"+ obj["code"].ToString());
  120. }
  121. }
  122. public void SetRoomId(string id)
  123. {
  124. MQTTManager.Instance.roomId = id;
  125. // MQTTClient.Instance.roomId = "9527";
  126. // 订阅房间频道
  127. MQTTClient.Instance.Subscribe(MQTTManager.Instance.front + "room/" + id);
  128. }
  129. public void UnSubscribe( string id)
  130. {
  131. MQTTClient.Instance.UnSubscribe(MQTTManager.Instance.front + "room/" + id);
  132. }
  133. /// <summary>
  134. /// 发送消息
  135. /// </summary>
  136. /// <param name="msg"></param>
  137. public void PushMsg(string msg)
  138. {
  139. if (!string.IsNullOrEmpty(msg))
  140. {
  141. JsonData json = new JsonData();
  142. json["account"] = MQTTManager.Instance.account;
  143. json["Sid"] = "sid";//选择的场景id
  144. json["msg"] = msg;
  145. MQTTClient.Instance.publish(Encoding.UTF8.GetBytes(json.ToJson()));
  146. }
  147. }
  148. /// <summary>
  149. /// 发送消息
  150. /// </summary>
  151. /// <param name="msg"></param>
  152. public void PushMsg(string channelID, string msg)
  153. {
  154. Debug.Log("DGJ ===> MQTT Test "+ msg);
  155. if (!string.IsNullOrEmpty(msg))
  156. {
  157. MQTTClient.Instance.publish(channelID, Encoding.UTF8.GetBytes(msg));
  158. }
  159. }
  160. /// <summary>
  161. /// 接受消息
  162. /// </summary>
  163. /// <param name="msg"></param>
  164. public void Received(string msg)
  165. {
  166. JObject obj = JObject.Parse(msg);
  167. // Debug.Log("Received ===> " + obj["action"].ToString());
  168. if(obj["action"]!=null)
  169. switch (obj["action"].ToString())
  170. {
  171. case "timeout": // 超时
  172. UICallManager.Instance.EventTimeOut();
  173. break;
  174. case "invite": // 邀请
  175. if(obj["control"] !=null)
  176. {
  177. string id = obj["control"]["id"].ToString();
  178. RTCRoomManager.Instance.controlId = id;
  179. if(RTCRoomManager.Instance.rtcState != RTCEnum.OFF) // 当前已经在房间内 直接发送当前房间号
  180. {
  181. JsonData data = new JsonData();
  182. data["action"] = "invite";
  183. data["rtcRoom"] =RTCRoomManager.Instance.mRTCRoomInfo.roomId.ToString();
  184. data["personnel"] = new JsonData();
  185. data["personnel"]["id"] = MQTTManager.Instance.id;
  186. data["personnel"]["name"] = MQTTManager.Instance._username;
  187. //data["personnel"]["resourcePoo1"] = MQTTManager.Instance.resourcePool;
  188. data["personnel"]["resourcePoolId"] = MQTTManager.Instance.resourcePoolId;
  189. data["personnel"]["room"] = MQTTManager.Instance.room;
  190. data["personnel"]["phone"] = MQTTManager.Instance.phone;
  191. // 用MQTT 向中考发送邀请
  192. MQTTManager.Instance.PushMsg(MQTTManager.Instance.front + "online/" + RTCRoomManager.Instance.controlId, data.ToJson());
  193. }
  194. else // 不在房间内,创建房间后 发送邀请
  195. {
  196. //string roomid = obj["rtcRoom"].ToString();
  197. //string resourcePool = obj["control"]["resourcePoo1"].ToString();
  198. RTCRoomManager.Instance.isInvite = true;
  199. UICallManager.Instance.EventInvite(obj["control"].ToString(), "");
  200. Debug.Log(phone);
  201. }
  202. }
  203. else
  204. {
  205. Debug.LogError(" 非控制台邀请 ");
  206. }
  207. break;
  208. case "noExpert": // 无专家
  209. UICallManager.Instance.EventNoExpert();
  210. break;
  211. case "chat": // 聊天消息
  212. // 拆解用户名 和 聊天内容 显示到聊天框里
  213. string username = obj["from"].ToString();
  214. string chatMsg = obj["chatMessage"].ToString();
  215. FileList.Instance.AddMsgItem(null, username, chatMsg);
  216. break;
  217. case "share": // 接受文件
  218. Debug.Log(" DGJ ===> share " + obj["chatMessage"].ToString());
  219. // 拆解用户名 和 文件名称 显示到聊天框里 下载文件
  220. FileConfig fileConfig = JsonConvert.DeserializeObject<FileConfig>(obj["chatMessage"].ToString());
  221. fileConfig.From = obj["from"].ToString();
  222. Debug.Log(" DGJ ===> FileConfig " + JsonConvert.SerializeObject(fileConfig));
  223. FileList.Instance.AddItem(fileConfig);
  224. break;
  225. case "remark": // 空间标注
  226. float Valuex = float.Parse(obj["chatMessage"]["x"].ToString());
  227. float Valuey = float.Parse(obj["chatMessage"]["y"].ToString());
  228. ArrowList.Instance.AddArrow(Valuex, Valuey);
  229. break;
  230. case "clearRemark":
  231. ArrowList.Instance.DeleteAll();
  232. break;
  233. case "leave":
  234. // 退出房间
  235. string roleId = obj["roleId"].ToString();
  236. if (roleId == "2")
  237. {
  238. // RTCRoomManager.Instance.UserOffline(UserEnum.Expert);
  239. }
  240. if (roleId == "1")
  241. {
  242. // RTCRoomManager.Instance.UserOffline(UserEnum.Conterl);
  243. }
  244. break;
  245. case "stop":
  246. // 停止同步位置信息
  247. SynPlayerTransform.Instance.isPushPos = false;
  248. break;
  249. default:
  250. break;
  251. }
  252. if (obj["image_data"] != null)
  253. {
  254. SynPlayerTransform.Instance.isPushPos = true;
  255. LangChaoMailManager.Instance.ReceiverMail(obj);
  256. }
  257. }
  258. }