1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 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<TestMQTT>
- {
- //public Text text;
- //public InputField input;
- //public Button btn;
- private void Start()
- {
- // btn.onClick.AddListener(PushMsg);
- }
- /// <summary>
- /// 获取信令
- /// </summary>
- 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;
- }
- }
- }
|