using LitJson; using login; using System; using System.Collections; using System.Collections.Generic; using System.Text; using UnityEngine; using UnityEngine.Android; using uPLibrary.Networking.M2Mqtt.Messages; using static QTTManager; public class MQTTClient : MonoSingleton { //public string front = "client/"; // public string id = ""; //public string username = "u@unity3";// 需要根据服务器设置 //public string password = null;// 需要根据服务器设置 public string rid = "mqttx_b4c02ddc"; //其他人的ID //public string account; //public string roomId; //public string email; //public string phone; //public string roleId; //public string resourcePool; //public string expertType; //public string _username; public event Action OnCoordinate; public static byte[] bytes; // public static string certification; // public static byt ta; QTTManager qt; // Start is called before the first frame update void Start() { // 请求文件读取和写入权限 if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead) || !Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite)) { Permission.RequestUserPermission(Permission.ExternalStorageRead); Permission.RequestUserPermission(Permission.ExternalStorageWrite); } bytes = (Resources.Load("emqxsl-ca") as TextAsset).bytes; Debug.Log(" DGJ ===> emqxsl-ca.bytes " + bytes.Length); } Queue rlist = new Queue(); private void OnReceived(MqttMsgPublishEventArgs obj) { rlist.Enqueue(obj); } private void OnConnecting() { Debug.Log("MQtt 连接中"); } private void onSucceed() { // Subscribe(MQTTManager.Instance.front + "online/" + MQTTManager.Instance.phone); Subscribe(MQTTManager.Instance.front + "personnel/" + MQTTManager.Instance.resourcePool); Subscribe(MQTTManager.Instance.front + "online/" + MQTTManager.Instance.phone); } 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()); } } } //MQTT接收到的数据 public void OnUnityReceived(MqttMsgPublishEventArgs obj) { string msg = Encoding.UTF8.GetString(obj.Message); Debug.Log("uid => " + obj.Topic + ":\n" + msg); // UILogManager.Instance.text3.text = msg; MQTTManager.Instance.Received(msg); } private void OnDestroy() { DisConnect(); } //连接 public void Connect() { Debug.Log("DGJ ===> MQTT " + HttpActionLang.Instance.mqttSocket ); //if (DeviceType.type == "Phone") //{ // id =UserInfo.Instance.name + "_Phone"; // rid = front+UserInfo.Instance.name + "_Glasses"; //} //else //{ // id = UserInfo.Instance.name + "_Glasses"; // rid = front+UserInfo.Instance.name + "_Phone"; //} // Debug.Log("DGJ ==>MQTT " + id + account + " " + username + " " + certification + " " + HttpActionLang.Instance.mqttSocket); DisConnect(); qt = new QTTManager(MQTTManager.Instance.id, MQTTManager.Instance.username , MQTTManager.certification, HttpActionLang.Instance.mqttSocket, "1883"); qt.Connect(); StartCoroutine(Reconnection()); qt.ConnectionFailed += onFaild; qt.ConnectionSucceeded += onSucceed; qt.OnConnecting += OnConnecting; qt.OnReceived += OnReceived; qt.OnClose += OnClose; // StartSendCameraPos(); } private void OnClose(EventArgs obj) { Debug.Log("断开连接"); } private IEnumerator Reconnection() { while (true) { yield return new WaitForSeconds(5); if (qt != null && !qt.IsConnect()) { Debug.Log(" DGJ ===> Reconnection "); Connect(); } } } //断开连接 public void DisConnect() { if (qt != null && qt.IsConnect()) qt.DisConnect(); } /// /// 订阅频道 /// /// public void Subscribe(string channelID) { Debug.Log(" DGJ =====> Subscribe " + channelID); if (qt != null && qt.IsConnect()) { ushort s = qt.Subscribe( new string[] { channelID //front+"room/"+roomId }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, //MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); Debug.Log(" DGJ =====> Subscribe2"); } else Debug.LogError(" MQTT 未连接 !!!"); } public void StartSendCameraPos() { StartCoroutine(sendCameraPos()); } //眼镜端像手机端发送实时位置 IEnumerator sendCameraPos() { Debug.Log("DGJ =====> sendCameraPos" + qt.IsConnect()); while (true) { yield return new WaitForSeconds(1f); if (qt != null && 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["x"] = GameManager.Instance.Player.transform.localPosition.x; //json["y"] = GameManager.Instance.Player.transform.localPosition.y; //json["z"] = GameManager.Instance.Player.transform.localPosition.z; json["Sid"] = "sid";//选择的场景id publish(Encoding.UTF8.GetBytes(json.ToJson())); } } } //眼镜发送触发的景点 public void sendActiveSp(string spid, bool isOpen) { JsonData json = new JsonData(); json["type"] = "ActiveSp"; json["Sid"] = "sid";//选择的场景id json["spid"] = spid;//景点id 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";//选择的场景id json["spid"] = spid;//景点id json["videoId"] = videoId;//视频id json["isplay"] = isplay;//是否播放 json["jindu"] = jindu;//视频进度 publish(Encoding.UTF8.GetBytes(json.ToJson())); Debug.Log("MQtt 发送manage"); } public void SendCoord() { JsonData json = new JsonData(); json["id"] = UserInfo.Instance.name; json["type"] = "Coord"; json["x"] = 0.1f; json["y"] = 0.1f; json["z"] = 0.1f; } public void publish(byte[] bs) { if (qt != null && qt.IsConnect()) { // Debug.Log(id + account + " DGJ publish =====> " + front + "room/" + roomId + " " + bs.Length); // qt.Publish(front + "room/" + roomId, bs, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); } else { Debug.LogError(" MQTT 未连接 "); } } public void publish(string channelID, byte[] bs) { if (qt != null && qt.IsConnect()) { Debug.Log(MQTTManager.Instance.id + MQTTManager.Instance.account + " DGJ publish =====> " + channelID + " " + bs.Length); qt.Publish(channelID, bs, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); } else { Debug.LogError(" MQTT 未连接 "); } } }