MQTT Connect
This commit is contained in:
8
Assets/StreamingAssets.meta
Normal file
8
Assets/StreamingAssets.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: af819185787d8dd48b7dc699e7d4403a
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
9
Assets/StreamingAssets/MQTTConfig.json
Normal file
9
Assets/StreamingAssets/MQTTConfig.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"configs": [
|
||||
{
|
||||
"name": "main",
|
||||
"host": "220.90.135.42",
|
||||
"port": 3018
|
||||
}
|
||||
]
|
||||
}
|
||||
7
Assets/StreamingAssets/MQTTConfig.json.meta
Normal file
7
Assets/StreamingAssets/MQTTConfig.json.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2c9fb26d616e68a4a97e4668f7190990
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/WorkSpace.meta
Normal file
8
Assets/WorkSpace.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 69728f35d447ee4419c4a601d59a38ea
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/WorkSpace/LH.meta
Normal file
8
Assets/WorkSpace/LH.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 853be44bcfbea934c8ef1209baced936
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
17
Assets/WorkSpace/LH/MQTTConfig.cs
Normal file
17
Assets/WorkSpace/LH/MQTTConfig.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Octopus.Simulator.Networks
|
||||
{
|
||||
public class MQTTConfigList
|
||||
{
|
||||
public List<MQTTConfig> configs=new List<MQTTConfig>();
|
||||
}
|
||||
|
||||
public class MQTTConfig
|
||||
{
|
||||
public string name;
|
||||
public string host;
|
||||
public int port;
|
||||
}
|
||||
}
|
||||
2
Assets/WorkSpace/LH/MQTTConfig.cs.meta
Normal file
2
Assets/WorkSpace/LH/MQTTConfig.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c3704dcef3ce96e41b03657cf842f32e
|
||||
124
Assets/WorkSpace/LH/MQTTManager.cs
Normal file
124
Assets/WorkSpace/LH/MQTTManager.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using UnityEngine;
|
||||
using Best.MQTT;
|
||||
using Best.MQTT.Packets;
|
||||
using Best.MQTT.Packets.Builders;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
|
||||
namespace Octopus.Simulator.Networks
|
||||
{
|
||||
public class MQTTManager : MonoBehaviour
|
||||
{
|
||||
public static MQTTManager mqttManager;
|
||||
|
||||
public Dictionary<string, MQTTClient> clientTable = new Dictionary<string, MQTTClient>();
|
||||
|
||||
[SerializeField]
|
||||
string MQTTpath = "";
|
||||
MQTTClient client;
|
||||
|
||||
public event Action onConnectedEvent;
|
||||
public event Action<DisconnectReasonCodes, string> onDisconnectedEvent;
|
||||
public event Action<string, string> onMessageReceived;
|
||||
public event Action<string> onErrorEvent;
|
||||
public event Action onCompletedTimerEvent;
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Awake()
|
||||
{
|
||||
mqttManager = this;
|
||||
SetMqttConfig();
|
||||
}
|
||||
|
||||
private void SetMqttConfig()
|
||||
{
|
||||
string path = Application.streamingAssetsPath + MQTTpath;
|
||||
string json;
|
||||
using (StreamReader reader = new StreamReader(path))
|
||||
{
|
||||
json = reader.ReadToEnd();
|
||||
}
|
||||
MQTTConfigList ConfigList = JsonConvert.DeserializeObject<MQTTConfigList>(json);
|
||||
foreach (var config in ConfigList.configs)
|
||||
{
|
||||
string clientName = config.name;
|
||||
string host = config.host;
|
||||
int port = config.port;
|
||||
|
||||
Connect(clientName, host,port);
|
||||
}
|
||||
}
|
||||
|
||||
public void SubscriptionTopic(MQTTClient client, string topic)
|
||||
{
|
||||
client.AddTopicAlias(topic);
|
||||
|
||||
client.CreateSubscriptionBuilder(topic)
|
||||
.WithMessageCallback(OnMessage)
|
||||
.WithMaximumQoS(QoSLevels.ExactlyOnceDelivery)
|
||||
.BeginSubscribe();
|
||||
|
||||
}
|
||||
|
||||
public void Connect(string clientName, string host,int port)
|
||||
{
|
||||
client = new MQTTClientBuilder()
|
||||
.WithOptions(new ConnectionOptionsBuilder().WithTCP(host, port))
|
||||
.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)
|
||||
{
|
||||
Debug.Log("connect");
|
||||
clientTable.Add(clientName, client);
|
||||
onConnected?.Invoke(client);
|
||||
SubscriptionTopic(client, "simulation/435faabb-6183-4690-a81f-027dd96d5827/#");
|
||||
}
|
||||
|
||||
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);
|
||||
onMessageReceived?.Invoke(topicName, payload);
|
||||
}
|
||||
|
||||
private void OnDisconnected(MQTTClient client, DisconnectReasonCodes reasonCode, string reasonMessage)
|
||||
{
|
||||
Debug.Log(reasonMessage);
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private void OnStateChange(MQTTClient client, ClientStates oldState, ClientStates newState)
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
client?.CreateDisconnectPacketBuilder()
|
||||
.WithReasonCode(DisconnectReasonCodes.NormalDisconnection)
|
||||
//.WithReasonString("Bye")
|
||||
.BeginDisconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/WorkSpace/LH/MQTTManager.cs.meta
Normal file
2
Assets/WorkSpace/LH/MQTTManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6c81b4b1ce2fb4345950d24862ca380c
|
||||
Reference in New Issue
Block a user