MQTTClient.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. switch (data["type"].ToString())
  58. {
  59. case "CamPos":
  60. // 根据 uid 同步Player 位置 如果没有就创建
  61. MultiPlayerManager.Instance.ReceivedCamPos(obj.Topic, data);
  62. break;
  63. case "ActiveSp":
  64. // 同步对应景点的开关
  65. MultiPlayerManager.Instance.ReceivedActiveSp( data);
  66. break;
  67. case "ActiveVideo":
  68. // 同步播放器的对应状态
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. catch
  75. {
  76. return;
  77. }
  78. }
  79. private void OnDestroy()
  80. {
  81. DisConnect();
  82. }
  83. //连接
  84. public void Connect()
  85. {
  86. if (DeviceType.type == "Phone")
  87. {
  88. id =UserInfo.Instance.Account + "_Phone";
  89. rid = UserInfo.Instance.Account + "_Glasses";
  90. }
  91. else
  92. {
  93. id = UserInfo.Instance.Account + "_Glasses";
  94. rid = UserInfo.Instance.Account + "_Phone";
  95. }
  96. DisConnect();
  97. qt = new QTTManager(id, username, password);
  98. qt.Connect();
  99. qt.ConnectionFailed += onFaild;
  100. qt.ConnectionSucceeded += onSucceed;
  101. qt.OnConnecting += OnConnecting;
  102. qt.OnReceived += OnReceived;
  103. qt.OnClose += OnClose;
  104. }
  105. private void OnClose(EventArgs obj)
  106. {
  107. Debug.Log("断开连接");
  108. }
  109. //断开连接
  110. public void DisConnect()
  111. {
  112. if (qt != null && qt.IsConnect())
  113. qt.DisConnect();
  114. }
  115. //订阅
  116. public void Subscribe()
  117. {
  118. ushort s = qt.Subscribe(
  119. new string[]
  120. {
  121. "client/manage",
  122. front+id
  123. },
  124. new byte[]
  125. {
  126. MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE ,
  127. MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE
  128. });
  129. StartCoroutine(sendCameraPos());
  130. }
  131. //眼镜端像手机端发送实时位置
  132. IEnumerator sendCameraPos()
  133. {
  134. while (qt.IsConnect())
  135. {
  136. JsonData json = new JsonData();
  137. json["type"] = "CamPos";
  138. json["x"] = OpenXRCamera.Instance.head.position.x;
  139. json["y"] = OpenXRCamera.Instance.head.position.y;
  140. json["z"] = OpenXRCamera.Instance.head.position.z;
  141. json["Sid"] = "sid";//选择的场景id
  142. publish(Encoding.UTF8.GetBytes(json.ToJson()));
  143. yield return new WaitForSeconds(0.1f);
  144. }
  145. }
  146. //眼镜发送触发的景点
  147. public void sendActiveSp(string spid,bool isOpen)
  148. {
  149. JsonData json = new JsonData();
  150. json["type"] = "ActiveSp";
  151. json["Sid"] = "sid";//选择的场景id
  152. json["spid"] = spid;//景点id
  153. json["isOpen"] = isOpen;//是否打开
  154. publish(Encoding.UTF8.GetBytes(json.ToJson()));
  155. Debug.Log("MQtt 发送manage");
  156. }
  157. //眼镜发送触发的视频
  158. public void sendActiveVideo(string spid,string videoId, bool isplay,float jindu)
  159. {
  160. JsonData json = new JsonData();
  161. json["type"] = "ActiveVideo";
  162. json["Sid"] = "sid";//选择的场景id
  163. json["spid"] = spid;//景点id
  164. json["videoId"] = videoId;//视频id
  165. json["isplay"] = isplay;//是否播放
  166. json["jindu"] = jindu;//视频进度
  167. publish(Encoding.UTF8.GetBytes(json.ToJson()));
  168. Debug.Log("MQtt 发送manage");
  169. }
  170. void publish(byte[] bs)
  171. {
  172. qt.Publish(front + rid, bs, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
  173. }
  174. }