first commit
This commit is contained in:
46
Assets/Script/mqtt/MqttClientController.cs
Normal file
46
Assets/Script/mqtt/MqttClientController.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Script/mqtt/MqttClientController.cs.meta
Normal file
11
Assets/Script/mqtt/MqttClientController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b4a12d2d54e4c53428426127da4b9c51
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
30
Assets/Script/mqtt/MqttOJ.cs
Normal file
30
Assets/Script/mqtt/MqttOJ.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using MQTTnet;
|
||||
using MQTTnet.Protocol;
|
||||
using UnityEngine;
|
||||
|
||||
public class MqttOJ : MonoBehaviour
|
||||
{
|
||||
public MqttClientController mqttClientController;
|
||||
public string topic = "unity/position";
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (mqttClientController != null && mqttClientController.IsConnected)
|
||||
{
|
||||
Vector3 position = transform.position;
|
||||
string message = JsonUtility.ToJson(position);
|
||||
|
||||
var mqttMessage = new MqttApplicationMessageBuilder()
|
||||
.WithTopic(topic)
|
||||
.WithPayload(message)
|
||||
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce)
|
||||
.Build();
|
||||
|
||||
mqttClientController.mqttClient.PublishAsync(mqttMessage);
|
||||
}
|
||||
else
|
||||
{
|
||||
//Debug.LogWarning("MQTT client is not connected. Skipping publish.");
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Script/mqtt/MqttOJ.cs.meta
Normal file
11
Assets/Script/mqtt/MqttOJ.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3925dfe133e555c4d8cd03541df378b5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
54
Assets/Script/mqtt/MqttReceiver.cs
Normal file
54
Assets/Script/mqtt/MqttReceiver.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
public class MqttUnityClient : MonoBehaviour
|
||||
{
|
||||
private IMqttClient _mqttClient;
|
||||
|
||||
private async void Start()
|
||||
{
|
||||
await ConnectAsync();
|
||||
// Đăng ký chủ đề để nhận tin nhắn
|
||||
await _mqttClient.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic("unity/topic").Build());
|
||||
}
|
||||
|
||||
private async Task ConnectAsync()
|
||||
{
|
||||
var factory = new MqttFactory();
|
||||
_mqttClient = factory.CreateMqttClient();
|
||||
|
||||
var options = new MqttClientOptionsBuilder()
|
||||
.WithClientId("UnityClient")
|
||||
.WithTcpServer("broker.hivemq.com", 1883)
|
||||
.WithCleanSession()
|
||||
.Build();
|
||||
|
||||
_mqttClient.ApplicationMessageReceivedAsync += e =>
|
||||
{
|
||||
// Xử lý tin nhắn nhận được
|
||||
var message = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.PayloadSegment);
|
||||
Debug.Log($"Received: {message}");
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
await _mqttClient.ConnectAsync(options);
|
||||
}
|
||||
|
||||
public async Task PublishAsync(string topic, string payload)
|
||||
{
|
||||
var message = new MqttApplicationMessageBuilder()
|
||||
.WithTopic(topic)
|
||||
.WithPayload(payload)
|
||||
.WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce)
|
||||
.Build();
|
||||
|
||||
if (_mqttClient.IsConnected)
|
||||
{
|
||||
await _mqttClient.PublishAsync(message);
|
||||
Debug.Log($"Published: {payload} to {topic}");
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Script/mqtt/MqttReceiver.cs.meta
Normal file
11
Assets/Script/mqtt/MqttReceiver.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6c7b03c5c34c4e840b9fcf3204cdb029
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user