| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using System.Collections;
- using System.Collections.Generic;
- using System.Globalization;
- using UnityEngine;
- using Rocworks.Mqtt;
- namespace ToneTuneToolkit.MQTT
- {
- public class MQTTManager : MonoBehaviour
- {
- public static MQTTManager Instance;
- #region Path
- private string configPath = $"{Application.streamingAssetsPath}/configs/mqttconfig.json";
- #endregion
- public MqttClient MqttClient;
- // ==================================================
- private void Awake()
- {
- Instance = this;
- }
- private void Start()
- {
- Init();
- }
- // private void OnApplicationQuit()
- // {
- // Uninit();
- // }
- // ==================================================
- private void Init()
- {
- // MqttClient.Host = JsonManager.GetJson(configPath, "host");
- // MqttClient.Port = JsonManager.GetJson(configPath, "port");
- return;
- }
- // private void Uninit()
- // {
- // return;
- // }
- // ==================================================
- public void SetMQTTClientHost(string value)
- {
- MqttClient.Host = value;
- return;
- }
- public void SetMQTTClientPort(int value)
- {
- MqttClient.Port = value;
- return;
- }
- // ==================================================
- /// <summary>
- /// 发送消息
- /// </summary>
- /// <param name="topic"></param>
- /// <param name="message"></param>
- public void SendMessageOut(string topic, string message)
- {
- MqttClient.Connection.Publish(topic, message);
- Debug.Log($"[MQTT Manager] Message [<color=white>{message}</color>] send to [<color=white>{MqttClient.Host}:{MqttClient.Port}</color>].");
- return;
- }
- /// <summary>
- /// 接收消息
- /// </summary>
- /// <param name="value"></param>
- public void OnMessageArrived(MqttMessage value)
- {
- Debug.Log($"[MQTT Manager] Message [<color=white>{value}</color>] received.");
- return;
- }
- }
- }
|