first commit

This commit is contained in:
lethanhsonvsp
2025-11-17 15:36:52 +07:00
commit 6f2eafa33c
14093 changed files with 1253472 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
using UnityEngine;
using MQTTnet;
using MQTTnet.Client;
using System.Threading.Tasks;
using System.Text;
public class MqttClientController : MonoBehaviour
{
public string mqttBrokerAddress = "broker.hivemq.com";
public int mqttPort = 1883;
public IMqttClient mqttClient { get; private set; }
public bool IsConnected => mqttClient?.IsConnected ?? false;
async void Start()
{
var factory = new MqttFactory();
mqttClient = factory.CreateMqttClient();
var options = new MqttClientOptionsBuilder()
.WithClientId("UnityClient")
.WithTcpServer(mqttBrokerAddress, mqttPort)
.WithCleanSession()
.Build();
mqttClient.ConnectedAsync += async e =>
{
Debug.Log("Connected to MQTT Broker.");
await Task.CompletedTask;
};
mqttClient.DisconnectedAsync += async e =>
{
Debug.Log("Disconnected from MQTT Broker.");
await Task.CompletedTask;
};
try
{
await mqttClient.ConnectAsync(options);
}
catch (System.Exception ex)
{
Debug.LogError($"MQTT connection failed: {ex.Message}");
}
}
}