MQTTTest.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections.Generic;
  2. using Blue;
  3. using UnityEngine;
  4. using uPLibrary.Networking.M2Mqtt.Messages;
  5. public class MQTTTest : AbstractController
  6. {
  7. private List<string> topics = new List<string>()
  8. {
  9. "client/manage",
  10. "client/mqttx_d4c106a3",
  11. "client/mqttx_b4c02ddc",
  12. "client/mqttx_ab869d21"
  13. };
  14. private string[] topicSub = new string[4]
  15. {
  16. "client/manage",
  17. "client/mqttx_d4c106a3",
  18. "client/mqttx_b4c02ddc",
  19. "client/mqttx_ab869d21"
  20. };
  21. private IMQTTService mQTTService;
  22. /*
  23. 1883 MQTT 协议端口
  24. 8883 MQTT/SSL 端口
  25. 8083 MQTT/WebSocket 端口
  26. 8080 HTTP API 端口
  27. 18083 Dashboard 管理控制台端口
  28. */
  29. private void Start()
  30. {
  31. mQTTService = this.GetService<IMQTTService>();
  32. mQTTService.brokerAddress = "api-fat1.ghz-tech.com";
  33. mQTTService.brokerPort = 1883;
  34. mQTTService.mqttUserName = "u@unity3";
  35. }
  36. void Update()
  37. {
  38. mQTTService.Update();
  39. if(Input.GetKeyDown(KeyCode.A))
  40. {
  41. mQTTService.SubscribeTopics(topicSub, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE , MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE , MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
  42. }
  43. else if(Input.GetKeyDown(KeyCode.S))
  44. {
  45. mQTTService.UnsubscribeTopics(topicSub);
  46. }
  47. if(Input.GetKeyDown(KeyCode.D))
  48. {
  49. mQTTService.Connect();
  50. }
  51. if(Input.GetKeyDown(KeyCode.Q))
  52. {
  53. this.SendCommand(new MQTTCommand(topics[0],"KeyCode.Q"));
  54. }
  55. else if(Input.GetKeyDown(KeyCode.W))
  56. {
  57. this.SendCommand(new MQTTCommand(topics[1],"KeyCode.W"));
  58. }
  59. else if(Input.GetKeyDown(KeyCode.E))
  60. {
  61. this.SendCommand(new MQTTCommand(topics[2],"KeyCode.E"));
  62. }
  63. else if(Input.GetKeyDown(KeyCode.R))
  64. {
  65. this.SendCommand(new MQTTCommand(topics[3],"KeyCode.R"));
  66. }
  67. }
  68. private void OnApplicationQuit()
  69. {
  70. mQTTService.OnApplicationQuit();
  71. }
  72. }