123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- using LitJson;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- using uPLibrary.Networking.M2Mqtt.Messages;
- using static QTTManager;
- public class MQTTClient : MonoSingleton<MQTTClient>
- {
- string front = "client/";
- string id = "";
- string username = "u@unity1";
- string password = null;
- string rid = "mqttx_b4c02ddc";
- QTTManager qt;
-
- 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()
- {
- Subscribe();
- }
- private void onFaild()
- {
- Debug.Log("MQtt 连接失败");
- }
-
- 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);
- try
- {
- JsonData data = JsonMapper.ToObject(msg);
- switch (data["type"].ToString())
- {
- case "CamPos":
-
- MultiPlayerManager.Instance.ReceivedCamPos(obj.Topic, data);
- break;
- case "ActiveSp":
-
- MultiPlayerManager.Instance.ReceivedActiveSp( data);
- break;
- case "ActiveVideo":
-
- break;
- default:
- break;
- }
- }
- catch
- {
- return;
- }
- }
- private void OnDestroy()
- {
- DisConnect();
- }
-
- public void Connect()
- {
- if (DeviceType.type == "Phone")
- {
- id =UserInfo.Instance.Account + "_Phone";
- rid = UserInfo.Instance.Account + "_Glasses";
- }
- else
- {
- id = UserInfo.Instance.Account + "_Glasses";
- rid = UserInfo.Instance.Account + "_Phone";
- }
- DisConnect();
- qt = new QTTManager(id, username, password);
- qt.Connect();
- 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",
- front+id
- },
- new byte[]
- {
- MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE ,
- MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE
- });
- StartCoroutine(sendCameraPos());
- }
-
- IEnumerator sendCameraPos()
- {
- while (qt.IsConnect())
- {
- JsonData json = new JsonData();
- json["type"] = "CamPos";
- json["x"] = OpenXRCamera.Instance.head.position.x;
- json["y"] = OpenXRCamera.Instance.head.position.y;
- json["z"] = OpenXRCamera.Instance.head.position.z;
- json["Sid"] = "sid";
- publish(Encoding.UTF8.GetBytes(json.ToJson()));
- yield return new WaitForSeconds(0.1f);
- }
- }
-
- public void sendActiveSp(string spid,bool isOpen)
- {
- JsonData json = new JsonData();
- json["type"] = "ActiveSp";
- json["Sid"] = "sid";
- json["spid"] = spid;
- json["isOpen"] = isOpen;
- publish(Encoding.UTF8.GetBytes(json.ToJson()));
- Debug.Log("MQtt 发送manage");
- }
-
- public void sendActiveVideo(string spid,string videoId, bool isplay,float jindu)
- {
- JsonData json = new JsonData();
- json["type"] = "ActiveVideo";
- json["Sid"] = "sid";
- json["spid"] = spid;
- json["videoId"] = videoId;
- json["isplay"] = isplay;
- json["jindu"] = jindu;
- publish(Encoding.UTF8.GetBytes(json.ToJson()));
- Debug.Log("MQtt 发送manage");
- }
- void publish(byte[] bs)
- {
- qt.Publish(front + rid, bs, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
- }
- }
|