123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- using uPLibrary.Networking.M2Mqtt.Messages;
- using static QTTManager;
- public class TestMqttDemo : MonoBehaviour
- {
- string id = "mqttx_d4c106a3";
- string rid = "mqttx_b4c02ddc";
- QTTManager qt;
- // Start is called before the first frame update
- void Start()
- {
- }
- Queue<MqttMsgPublishEventArgs> rlist = new Queue<MqttMsgPublishEventArgs>();
- private void OnReceived(MqttMsgPublishEventArgs obj)
- {
- rlist.Enqueue(obj);
- }
- private void OnConnecting()
- {
- Debug.Log("MQtt ������");
- }
- private void onSucceed()
- {
- }
- private void onFaild()
- {
- Debug.Log("MQtt ����ʧ��");
- }
- // Update is called once per frame
- void Update()
- {
- if (rlist.Count > 0)
- {
- for (int i = 0; i < rlist.Count; i++)
- {
- OnUnityReceived(rlist.Dequeue());
- }
- }
- }
- public void OnUnityReceived(MqttMsgPublishEventArgs obj)
- {
- string msg = Encoding.UTF8.GetString(obj.Message);
- Debug.Log("uid => " + obj.Topic + ":\n" + msg);
- }
- private void OnDestroy()
- {
- DisConnect();
- }
- //����
- public void Connect()
- {
- DisConnect();
- qt = new QTTManager(id, "u@unity1", null, "api-fat1.ghz-tech.com","1883");
- qt.Connect((Resources.Load("emqxsl-ca") as TextAsset).bytes);
- qt.ConnectionFailed += onFaild;
- qt.ConnectionSucceeded += onSucceed;
- qt.OnConnecting += OnConnecting;
- qt.OnReceived += OnReceived;
- qt.OnClose += OnClose;
- }
- private void OnClose(EventArgs obj)
- {
- Debug.Log("�Ͽ�����");
- }
- //�Ͽ�����
- public void DisConnect()
- {
- if (qt != null && qt.IsConnect())
- qt.DisConnect();
- }
- //����
- public void Subscribe()
- {
- ushort s = qt.Subscribe(
- new string[]
- {
- "client/manage",
- "client/"+id,
- "client/"+rid
- },
- new byte[]
- {
- MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE ,
- MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE,
- MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE
- });
- }
- //����1
- public void send()
- {
- qt.Publish("client/" + rid, Encoding.UTF8.GetBytes("111111111111111"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
- Debug.Log("MQtt ����" + rid);
- }
- //����2
- public void send2()
- {
- qt.Publish("client/manage", Encoding.UTF8.GetBytes("111111111111111"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
- Debug.Log("MQtt ����manage");
- }
- }
|