Files
XRLib/Assets/Scripts/mqtttest.cs
2025-11-11 09:42:47 +09:00

165 lines
4.6 KiB
C#

using Best.MQTT;
using Best.MQTT.Packets;
using Best.MQTT.Packets.Builders;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UVC.Factory.Alarm;
public class mqtttest : MonoBehaviour
{
public Dictionary<string, MQTTClient> clientTable = new Dictionary<string, MQTTClient>();
public event Action<string, string> onMessageReceived;
[SerializeField]
string MQTTpath = "";
MQTTClient client;
public bool isSceneReroad = false;
public event Action onLogicUpdated;
bool flag = true;
string protocol = "wss";
#if UNITY_EDITOR
private bool isManagerOver = false;
#endif
// Start is called once before the first execution of Update after the MonoBehaviour is created
private void Start()
{
Connect("test", "simulator.flexing.ai", 3049);
}
public void SubscribeTopic(string topic)
{
Debug.Log($"Try Subscribe {topic}");
client.AddTopicAlias(topic);
client.CreateSubscriptionBuilder(topic)
.WithMessageCallback(OnMessage)
.WithMaximumQoS(QoSLevels.ExactlyOnceDelivery)
.BeginSubscribe();
}
public void UnsubscribeTopic(string topic)
{
client.CreateUnsubscribePacketBuilder(topic).BeginUnsubscribe();
}
public void Connect(string clientName, string host, int port)
{
if (client != null)
{
flag = true;
clientTable.Remove(clientName);
client.OnConnected -= ((client) => OnConnected(client, clientName));
client.OnStateChanged -= OnStateChange;
client.OnDisconnect -= OnDisconnected;
client.OnError -= OnError;
}
if (string.Equals(protocol, "wss"))
{
client = new MQTTClientBuilder()
.WithOptions(new ConnectionOptionsBuilder().WithWebSocket(host, port).WithPath("/mqtt").WithTLS())
.WithEventHandler(client => OnConnected(client, clientName))
.WithEventHandler(OnStateChange)
.WithEventHandler(OnDisconnected)
.WithEventHandler(OnError)
.CreateClient();
}
else
{
client = new MQTTClientBuilder()
.WithOptions(new ConnectionOptionsBuilder().WithWebSocket(host, port).WithPath("/mqtt"))
.WithEventHandler(client => OnConnected(client, clientName))
.WithEventHandler(OnStateChange)
.WithEventHandler(OnDisconnected)
.WithEventHandler(OnError)
.CreateClient();
}
client.BeginConnect(ConnectPacketBuilderCallback);
}
public event Action<MQTTClient> onConnected;
private void OnConnected(MQTTClient client, string clientName)
{
clientTable.Add(clientName, client);
onConnected?.Invoke(client);
if (flag)
{
SubscribeTopic($"#");
flag = false;
}
}
private ConnectPacketBuilder ConnectPacketBuilderCallback(MQTTClient client, ConnectPacketBuilder builder)
{
return builder;
}
private void OnError(MQTTClient client, string error)
{
Debug.Log($"OnError! :{error}");
}
private void OnMessage(MQTTClient client, SubscriptionTopic topic, string topicName, ApplicationMessage message)
{
var payload = Encoding.UTF8.GetString(message.Payload.Data, message.Payload.Offset, message.Payload.Count);
Debug.Log(payload);
if (topicName.Contains("completed"))
{
return;
}
if (payload.Contains("component_id"))
{
}
if (topicName.Contains("middleware"))
{
onLogicUpdated?.Invoke();
Debug.Log("logicupdated");
}
}
private async void OnDisconnected(MQTTClient client, DisconnectReasonCodes reasonCode, string reasonMessage)
{
#if UNITY_EDITOR
if (isManagerOver)
return;
#endif
Debug.Log("들어옴");
if (isSceneReroad)
{
isSceneReroad = false;
return;
}
if (Application.isPlaying)
{
}
}
private void OnStateChange(MQTTClient client, ClientStates oldState, ClientStates newState)
{
//throw new NotImplementedException();
}
void OnDestroy()
{
#if UNITY_EDITOR
isManagerOver = true;
#endif
client?.CreateDisconnectPacketBuilder()
.WithReasonCode(DisconnectReasonCodes.NormalDisconnection)
.WithReasonString("Bye")
.BeginDisconnect();
}
}