MQTTManager.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using LitJson;
  2. using Newtonsoft.Json.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using UnityEngine;
  7. public class MQTTManager : MonoSingleton<MQTTManager>
  8. {
  9. /// <summary>
  10. /// 获取信令
  11. /// </summary>
  12. public void Signal()
  13. {
  14. GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.signal_certification, "", SignalCallBack));
  15. // GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.userinfo, "", UserInfoCallBack));
  16. // HttpTool.Instance.PostMqtt(HttpAction.signal_certification, "", SignalCallBack);
  17. }
  18. public void SetAccoutInfo(string account)
  19. {
  20. MQTTClient.Instance.account = account;
  21. }
  22. private void SignalCallBack(string msg)
  23. {
  24. Debug.Log(msg);
  25. JObject obj = JObject.Parse(msg);
  26. if (obj["code"].ToString() == "200")
  27. {
  28. HttpActionLang.Instance.mqttSocket = obj["data"]["host"].ToString();
  29. MQTTClient.certification = obj["data"]["certification"].ToString();
  30. MQTTClient.Instance.front = obj["data"]["topicPrefix"].ToString();
  31. MQTTClient.Instance.id = obj["data"]["clientId"].ToString();
  32. MQTTClient.Instance.username = obj["data"]["username"].ToString();
  33. MQTTClient.Instance.Connect();
  34. }
  35. else
  36. {
  37. Debug.LogError("MQTT 获取信令失败 !!!" + obj["code"].ToString());
  38. }
  39. }
  40. private void UserInfoCallBack(string msg)
  41. {
  42. Debug.Log(msg);
  43. JObject obj = JObject.Parse(msg);
  44. if (obj["code"].ToString() == "200")
  45. {
  46. MQTTClient.Instance._username = obj["data"]["name"].ToString();
  47. MQTTClient.Instance.roleId = obj["data"]["roleId"].ToString();
  48. MQTTClient.Instance.email = obj["data"]["email"].ToString();
  49. MQTTClient.Instance.phone = obj["data"]["phone"].ToString();
  50. MQTTClient.Instance.resourcePool = obj["data"]["resourcePool"].ToString();
  51. MQTTClient.Instance.expertType = obj["data"]["expertType"].ToString();
  52. }
  53. else
  54. {
  55. Debug.LogError("获取用户信息失败 !!!"+ obj["code"].ToString());
  56. }
  57. }
  58. public void SetRoomId(string id)
  59. {
  60. MQTTClient.Instance.roomId = id;
  61. // MQTTClient.Instance.roomId = "9527";
  62. // 订阅房间频道
  63. MQTTClient.Instance.Subscribe(MQTTClient.Instance.front + "room/" + id);
  64. }
  65. /// <summary>
  66. /// 发送消息
  67. /// </summary>
  68. /// <param name="msg"></param>
  69. public void PushMsg(string msg)
  70. {
  71. if (!string.IsNullOrEmpty(msg))
  72. {
  73. JsonData json = new JsonData();
  74. json["account"] = MQTTClient.Instance.account;
  75. json["Sid"] = "sid";//选择的场景id
  76. json["msg"] = msg;
  77. MQTTClient.Instance.publish(Encoding.UTF8.GetBytes(json.ToJson()));
  78. }
  79. }
  80. /// <summary>
  81. /// 接受消息
  82. /// </summary>
  83. /// <param name="msg"></param>
  84. public void Received(string msg)
  85. {
  86. JObject obj = JObject.Parse(msg);
  87. Debug.Log("Received ===> " + obj["action"].ToString());
  88. switch (obj["action"].ToString())
  89. {
  90. case "timeout": // 超时
  91. UICallManager.Instance.EventTimeOut();
  92. break;
  93. case "invite": // 邀请
  94. if(obj["control"]!=null)
  95. {
  96. string roomid = obj["rtcRoom"].ToString();
  97. UICallManager.Instance.EventInvite(obj["control"].ToString(), roomid);
  98. }
  99. else
  100. {
  101. Debug.LogError(" 非控制台邀请 ");
  102. }
  103. break;
  104. case "noexpert": // 无专家
  105. UICallManager.Instance.EventNoExpert();
  106. break;
  107. case "chat": // 聊天消息
  108. // 拆解用户名 和 聊天内容 显示到聊天框里
  109. string username = "";
  110. string chatMsg = "";
  111. FileList.Instance.AddMsgItem(null, username, chatMsg);
  112. break;
  113. case "file": // 接受文件
  114. // 拆解用户名 和 文件名称 显示到聊天框里 下载文件
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. }