TestMQTT.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using LitJson;
  2. using Newtonsoft.Json.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public class TestMQTT : MonoSingleton<TestMQTT>
  9. {
  10. //public Text text;
  11. //public InputField input;
  12. //public Button btn;
  13. private void Start()
  14. {
  15. // btn.onClick.AddListener(PushMsg);
  16. }
  17. /// <summary>
  18. /// 获取信令
  19. /// </summary>
  20. public void Signal()
  21. {
  22. GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.signal_certification, "", SignalCallBack));
  23. // HttpTool.Instance.PostMqtt(HttpAction.signal_certification, "", SignalCallBack);
  24. }
  25. public void SetAccoutInfo(string account)
  26. {
  27. MQTTClient.Instance.account = account;
  28. }
  29. private void SignalCallBack(string msg)
  30. {
  31. Debug.Log(msg);
  32. JObject obj = JObject.Parse(msg);
  33. if (obj["code"].ToString() == "200")
  34. {
  35. HttpActionLang.Instance.mqttSocket = obj["data"]["host"].ToString();
  36. MQTTClient.certification = obj["data"]["certification"].ToString();
  37. MQTTClient.Instance.front = obj["data"]["topicPrefix"].ToString();
  38. MQTTClient.Instance.id = obj["data"]["clientId"].ToString();
  39. MQTTClient.Instance.email = obj["data"]["email"].ToString();
  40. SetRoomId("");
  41. //
  42. }
  43. else
  44. {
  45. Debug.LogError("MQTT 获取信令失败 !!!");
  46. }
  47. }
  48. public void SetRoomId(string id)
  49. {
  50. MQTTClient.Instance.roomId = id;
  51. MQTTClient.Instance.roomId = "9527";
  52. MQTTClient.Instance.Connect();
  53. }
  54. public void PushMsg( string msg)
  55. {
  56. if(!string.IsNullOrEmpty(msg))
  57. {
  58. JsonData json = new JsonData();
  59. json["account"] = MQTTClient.Instance.account;
  60. json["Sid"] = "sid";//选择的场景id
  61. json["msg"] = msg;
  62. MQTTClient.Instance.publish(Encoding.UTF8.GetBytes(json.ToJson()));
  63. // input.text = null;
  64. }
  65. else
  66. {
  67. JsonData json = new JsonData();
  68. json["account"] = MQTTClient.Instance.account;
  69. json["Sid"] = "sid";//选择的场景id
  70. json["msg"] = "NULL";
  71. MQTTClient.Instance.publish(Encoding.UTF8.GetBytes(json.ToJson()));
  72. // input.text = null;
  73. }
  74. }
  75. }