using LitJson; using Newtonsoft.Json.Linq; using System.Collections; using System.Collections.Generic; using System.Text; using UnityEngine; using UnityEngine.UI; public class TestMQTT : MonoSingleton { //public Text text; //public InputField input; //public Button btn; private void Start() { // btn.onClick.AddListener(PushMsg); } /// /// 获取信令 /// public void Signal() { GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.signal_certification, "", SignalCallBack)); // HttpTool.Instance.PostMqtt(HttpAction.signal_certification, "", SignalCallBack); } public void SetAccoutInfo(string account) { MQTTClient.Instance.account = account; } private void SignalCallBack(string msg) { Debug.Log(msg); JObject obj = JObject.Parse(msg); if (obj["code"].ToString() == "200") { HttpActionLang.Instance.mqttSocket = obj["data"]["host"].ToString(); MQTTClient.certification = obj["data"]["certification"].ToString(); MQTTClient.Instance.front = obj["data"]["topicPrefix"].ToString(); MQTTClient.Instance.id = obj["data"]["clientId"].ToString(); MQTTClient.Instance.email = obj["data"]["email"].ToString(); SetRoomId(""); // } else { Debug.LogError("MQTT 获取信令失败 !!!"); } } public void SetRoomId(string id) { MQTTClient.Instance.roomId = id; MQTTClient.Instance.roomId = "9527"; MQTTClient.Instance.Connect(); } public void PushMsg( string msg) { if(!string.IsNullOrEmpty(msg)) { JsonData json = new JsonData(); json["account"] = MQTTClient.Instance.account; json["Sid"] = "sid";//选择的场景id json["msg"] = msg; MQTTClient.Instance.publish(Encoding.UTF8.GetBytes(json.ToJson())); // input.text = null; } else { JsonData json = new JsonData(); json["account"] = MQTTClient.Instance.account; json["Sid"] = "sid";//选择的场景id json["msg"] = "NULL"; MQTTClient.Instance.publish(Encoding.UTF8.GetBytes(json.ToJson())); // input.text = null; } } }