TestMqttDemo.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using UnityEngine;
  6. using uPLibrary.Networking.M2Mqtt.Messages;
  7. using static QTTManager;
  8. public class TestMqttDemo : MonoBehaviour
  9. {
  10. string id = "mqttx_d4c106a3";
  11. string rid = "mqttx_b4c02ddc";
  12. QTTManager qt;
  13. // Start is called before the first frame update
  14. void Start()
  15. {
  16. }
  17. Queue<MqttMsgPublishEventArgs> rlist = new Queue<MqttMsgPublishEventArgs>();
  18. private void OnReceived(MqttMsgPublishEventArgs obj)
  19. {
  20. rlist.Enqueue(obj);
  21. }
  22. private void OnConnecting()
  23. {
  24. Debug.Log("MQtt ������");
  25. }
  26. private void onSucceed()
  27. {
  28. }
  29. private void onFaild()
  30. {
  31. Debug.Log("MQtt ����ʧ��");
  32. }
  33. // Update is called once per frame
  34. void Update()
  35. {
  36. if (rlist.Count > 0)
  37. {
  38. for (int i = 0; i < rlist.Count; i++)
  39. {
  40. OnUnityReceived(rlist.Dequeue());
  41. }
  42. }
  43. }
  44. public void OnUnityReceived(MqttMsgPublishEventArgs obj)
  45. {
  46. string msg = Encoding.UTF8.GetString(obj.Message);
  47. Debug.Log("uid => " + obj.Topic + ":\n" + msg);
  48. }
  49. private void OnDestroy()
  50. {
  51. DisConnect();
  52. }
  53. //����
  54. public void Connect()
  55. {
  56. DisConnect();
  57. qt = new QTTManager(id, "u@unity1", null, "api-fat1.ghz-tech.com","1883");
  58. qt.Connect((Resources.Load("emqxsl-ca") as TextAsset).bytes);
  59. qt.ConnectionFailed += onFaild;
  60. qt.ConnectionSucceeded += onSucceed;
  61. qt.OnConnecting += OnConnecting;
  62. qt.OnReceived += OnReceived;
  63. qt.OnClose += OnClose;
  64. }
  65. private void OnClose(EventArgs obj)
  66. {
  67. Debug.Log("�Ͽ�����");
  68. }
  69. //�Ͽ�����
  70. public void DisConnect()
  71. {
  72. if (qt != null && qt.IsConnect())
  73. qt.DisConnect();
  74. }
  75. //����
  76. public void Subscribe()
  77. {
  78. ushort s = qt.Subscribe(
  79. new string[]
  80. {
  81. "client/manage",
  82. "client/"+id,
  83. "client/"+rid
  84. },
  85. new byte[]
  86. {
  87. MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE ,
  88. MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE,
  89. MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE
  90. });
  91. }
  92. //����1
  93. public void send()
  94. {
  95. qt.Publish("client/" + rid, Encoding.UTF8.GetBytes("111111111111111"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
  96. Debug.Log("MQtt ����" + rid);
  97. }
  98. //����2
  99. public void send2()
  100. {
  101. qt.Publish("client/manage", Encoding.UTF8.GetBytes("111111111111111"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
  102. Debug.Log("MQtt ����manage");
  103. }
  104. }