MQTTClient.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using LitJson;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using UnityEngine;
  7. using uPLibrary.Networking.M2Mqtt.Messages;
  8. using static QTTManager;
  9. public class MQTTClient : MonoSingleton<MQTTClient>
  10. {
  11. string front = "client/";
  12. string id = "";
  13. string username = "u@unity1";// 需要根据服务器设置
  14. string password = null;// 需要根据服务器设置
  15. string rid = "mqttx_b4c02ddc"; //其他人的ID
  16. QTTManager qt;
  17. // Start is called before the first frame update
  18. void Start()
  19. {
  20. }
  21. Queue<MqttMsgPublishEventArgs> rlist = new Queue<MqttMsgPublishEventArgs>();
  22. private void OnReceived(MqttMsgPublishEventArgs obj)
  23. {
  24. rlist.Enqueue(obj);
  25. }
  26. private void OnConnecting()
  27. {
  28. Debug.Log("MQtt 连接中");
  29. }
  30. private void onSucceed()
  31. {
  32. Subscribe();
  33. }
  34. private void onFaild()
  35. {
  36. Debug.Log("MQtt 连接失败");
  37. }
  38. // Update is called once per frame
  39. void Update()
  40. {
  41. if (rlist.Count > 0)
  42. {
  43. for (int i = 0; i < rlist.Count; i++)
  44. {
  45. OnUnityReceived(rlist.Dequeue());
  46. }
  47. }
  48. }
  49. //MQTT接收到的数据
  50. public void OnUnityReceived(MqttMsgPublishEventArgs obj)
  51. {
  52. string msg = Encoding.UTF8.GetString(obj.Message);
  53. Debug.Log("uid => " + obj.Topic + ":\n" + msg);
  54. try
  55. {
  56. JsonData data = JsonMapper.ToObject(msg);
  57. Debug.Log(" DGJ === > " + data["type"].ToString());
  58. switch (data["type"].ToString())
  59. {
  60. case "CamPos":
  61. Debug.Log("DGJ ===> CamPos");
  62. // 根据 uid 同步Player 位置 如果没有就创建
  63. MultiPlayerManager.Instance.ReceivedCamPos(obj.Topic, data);
  64. break;
  65. case "ActiveSp":
  66. // 同步对应景点的开关
  67. MultiPlayerManager.Instance.ReceivedActiveSp( data);
  68. break;
  69. case "ActiveVideo":
  70. // 同步播放器的对应状态
  71. break;
  72. default:
  73. break;
  74. }
  75. }
  76. catch
  77. {
  78. return;
  79. }
  80. }
  81. private void OnDestroy()
  82. {
  83. DisConnect();
  84. }
  85. //连接
  86. public void Connect()
  87. {
  88. if (DeviceType.type == "Phone")
  89. {
  90. id =UserInfo.Instance.Account + "_Phone";
  91. rid = UserInfo.Instance.Account + "_Glasses";
  92. }
  93. else
  94. {
  95. id = UserInfo.Instance.Account + "_Glasses";
  96. rid = UserInfo.Instance.Account + "_Phone";
  97. }
  98. DisConnect();
  99. qt = new QTTManager(id, username, password);
  100. qt.Connect();
  101. qt.ConnectionFailed += onFaild;
  102. qt.ConnectionSucceeded += onSucceed;
  103. qt.OnConnecting += OnConnecting;
  104. qt.OnReceived += OnReceived;
  105. qt.OnClose += OnClose;
  106. }
  107. private void OnClose(EventArgs obj)
  108. {
  109. Debug.Log("断开连接");
  110. }
  111. //断开连接
  112. public void DisConnect()
  113. {
  114. if (qt != null && qt.IsConnect())
  115. qt.DisConnect();
  116. }
  117. //订阅
  118. public void Subscribe()
  119. {
  120. Debug.Log(" DGJ =====> Subscribe");
  121. ushort s = qt.Subscribe(
  122. new string[]
  123. {
  124. "client/manage",
  125. front+id
  126. },
  127. new byte[]
  128. {
  129. MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE ,
  130. MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE
  131. });
  132. Debug.Log(" DGJ =====> Subscribe2");
  133. }
  134. public void StartSendCameraPos()
  135. {
  136. StartCoroutine(sendCameraPos());
  137. }
  138. //眼镜端像手机端发送实时位置
  139. IEnumerator sendCameraPos()
  140. {
  141. Debug.Log("DGJ =====> sendCameraPos" + qt.IsConnect());
  142. while (qt.IsConnect())
  143. {
  144. JsonData json = new JsonData();
  145. json["type"] = "CamPos";
  146. // json["x"] = OpenXRCamera.Instance.head.position.x;
  147. //json["y"] = OpenXRCamera.Instance.head.position.y;
  148. //json["z"] = OpenXRCamera.Instance.head.position.z;
  149. json["x"] = GameManager.Instance.Player.transform.localPosition.x;
  150. json["y"] = GameManager.Instance.Player.transform.localPosition.y;
  151. json["z"] = GameManager.Instance.Player.transform.localPosition.z;
  152. json["Sid"] = "sid";//选择的场景id
  153. publish(Encoding.UTF8.GetBytes(json.ToJson()));
  154. yield return new WaitForSeconds(0.1f);
  155. }
  156. }
  157. //眼镜发送触发的景点
  158. public void sendActiveSp(string spid,bool isOpen)
  159. {
  160. JsonData json = new JsonData();
  161. json["type"] = "ActiveSp";
  162. json["Sid"] = "sid";//选择的场景id
  163. json["spid"] = spid;//景点id
  164. json["isOpen"] = isOpen;//是否打开
  165. publish(Encoding.UTF8.GetBytes(json.ToJson()));
  166. Debug.Log("MQtt 发送manage");
  167. }
  168. //眼镜发送触发的视频
  169. public void sendActiveVideo(string spid,string videoId, bool isplay,float jindu)
  170. {
  171. JsonData json = new JsonData();
  172. json["type"] = "ActiveVideo";
  173. json["Sid"] = "sid";//选择的场景id
  174. json["spid"] = spid;//景点id
  175. json["videoId"] = videoId;//视频id
  176. json["isplay"] = isplay;//是否播放
  177. json["jindu"] = jindu;//视频进度
  178. publish(Encoding.UTF8.GetBytes(json.ToJson()));
  179. Debug.Log("MQtt 发送manage");
  180. }
  181. void publish(byte[] bs)
  182. {
  183. Debug.Log("DGJ publish =====> " +bs.Length);
  184. qt.Publish(front + rid, bs, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
  185. }
  186. }