MQTTManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 clientId = "";
  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 resourcePoolId;
  23. public string expertType;
  24. public string _username;
  25. public string room;
  26. public static string certification;
  27. /// <summary>
  28. /// 获取信令
  29. /// </summary>
  30. public void Signal()
  31. {
  32. // GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.signal_certification, "", SignalCallBack));
  33. // GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.userinfo, "", UserInfoCallBack));
  34. // HttpTool.Instance.PostMqtt(HttpAction.signal_certification, "", SignalCallBack);
  35. }
  36. public void SetAccoutInfo(string account)
  37. {
  38. MQTTManager.Instance.account = account;
  39. }
  40. private void SignalCallBack(string msg)
  41. {
  42. Debug.Log(msg);
  43. JObject obj = JObject.Parse(msg);
  44. if (obj["code"].ToString() == "200")
  45. {
  46. // HttpActionLang.Instance.mqttSocket = obj["data"]["host"].ToString();
  47. MQTTManager.certification = obj["data"]["certification"].ToString();
  48. MQTTManager.Instance.front = obj["data"]["topicPrefix"].ToString();
  49. MQTTManager.Instance.clientId = obj["data"]["clientId"].ToString();
  50. MQTTManager.Instance.username = obj["data"]["username"].ToString();
  51. // MQTTClient.Instance.Connect();
  52. // StartCoroutine(TestCreateRoom());
  53. }
  54. else
  55. {
  56. Debug.LogError("MQTT 获取信令失败 !!!" + obj["code"].ToString());
  57. }
  58. }
  59. private void UserInfoCallBack(string msg)
  60. {
  61. Debug.Log(msg);
  62. JObject obj = JObject.Parse(msg);
  63. if (obj["code"].ToString() == "200")
  64. {
  65. MQTTManager.Instance._username = obj["data"]["name"].ToString();
  66. MQTTManager.Instance.roleId = obj["data"]["roleId"].ToString();
  67. MQTTManager.Instance.email = obj["data"]["email"].ToString();
  68. MQTTManager.Instance.phone = obj["data"]["phone"].ToString();
  69. // MQTTManager.Instance.resourcePool = obj["data"]["resourcePool"].ToString();
  70. MQTTManager.Instance.resourcePoolId = obj["data"]["resourcePoolId"].ToString();
  71. MQTTManager.Instance.expertType = obj["data"]["expertType"].ToString();
  72. }
  73. else
  74. {
  75. Debug.LogError("获取用户信息失败 !!!" + obj["code"].ToString());
  76. }
  77. }
  78. public void SetRoomId(string id)
  79. {
  80. MQTTManager.Instance.roomId = id;
  81. // MQTTClient.Instance.roomId = "9527";
  82. // 订阅房间频道
  83. MQTTClient.Instance.Subscribe(MQTTManager.Instance.front + "room/" + id);
  84. }
  85. public void UnSubscribe(string id)
  86. {
  87. MQTTClient.Instance.UnSubscribe(MQTTManager.Instance.front + "room/" + id);
  88. }
  89. /// <summary>
  90. /// 发送消息
  91. /// </summary>
  92. /// <param name="msg"></param>
  93. public void PushMsg(string msg)
  94. {
  95. if (!string.IsNullOrEmpty(msg))
  96. {
  97. JsonData json = new JsonData();
  98. json["account"] = MQTTManager.Instance.account;
  99. json["Sid"] = "sid";//选择的场景id
  100. json["msg"] = msg;
  101. MQTTClient.Instance.publish(Encoding.UTF8.GetBytes(json.ToJson()));
  102. }
  103. }
  104. /// <summary>
  105. /// 发送消息
  106. /// </summary>
  107. /// <param name="msg"></param>
  108. public void PushMsg(string channelID, string msg)
  109. {
  110. Debug.Log("DGJ ===> MQTT Test " + msg);
  111. if (!string.IsNullOrEmpty(msg))
  112. {
  113. MQTTClient.Instance.publish(channelID, Encoding.UTF8.GetBytes(msg));
  114. }
  115. }
  116. /// <summary>
  117. /// 接受消息
  118. /// </summary>
  119. /// <param name="msg"></param>
  120. public void Received(string msg)
  121. {
  122. JObject obj = JObject.Parse(msg);
  123. Debug.Log("Received ===> " + obj["action"].ToString());
  124. switch (obj["action"].ToString())
  125. {
  126. default:
  127. break;
  128. }
  129. }
  130. }