Я пытаюсь разработать строку mqtt для моего проекта единства, используя m2mqttunity для подписки и получения сообщения от издателя. Я успешно подписался и получил сообщение и журнал. проблема в том, что я, кажется, не могу найти способ разобрать сообщение строки полезной нагрузки на float, чтобы я мог использовать float как вход температуры для моей демонстрации единства. ошибка, которую я получил:
System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) [0x00057] in <437ba245d8404784b9fbab9b439ac908>:0
at System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00013] in <437ba245d8404784b9fbab9b439ac908>:0
at System.Int32.Parse (System.String s) [0x00007] in <437ba245d8404784b9fbab9b439ac908>:0
at mqttTest.client_MqttMsgPublishReceived (System.Object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e) [0x00021] in C:\Users\Tila\Unity3d_MQTT-master\Assets\MQTT\scripts\test\mqttTest.cs:38
at uPLibrary.Networking.M2Mqtt.MqttClient.OnMqttMsgPublishReceived (uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublish publish) [0x0000f] in C:\Users\Tila\Unity3d_MQTT-master\Assets\MQTT\scripts\MqttClient.cs:764
at uPLibrary.Networking.M2Mqtt.MqttClient.ReceiveEventThread () [0x000f8] in C:\Users\Tila\Unity3d_MQTT-master\Assets\MQTT\scripts\MqttClient.cs:1487
at System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) [0x00014] in <437ba245d8404784b9fbab9b439ac908>:0
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in <437ba245d8404784b9fbab9b439ac908>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <437ba245d8404784b9fbab9b439ac908>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state) [0x0002b] in <437ba245d8404784b9fbab9b439ac908>:0
at System.Threading.ThreadHelper.ThreadStart () [0x00008] in <437ba245d8404784b9fbab9b439ac908>:0 occurred
, а GIT -
using UnityEngine;
using System.Collections;
using System.Net;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using uPLibrary.Networking.M2Mqtt.Utility;
using uPLibrary.Networking.M2Mqtt.Exceptions;
using System;
public class mqttTest : MonoBehaviour {
private MqttClient client;
public int tempDemo;
// Use this for initialization
void Start () {
// create client instance
client = new MqttClient(IPAddress.Parse("127.0.0.1"),1883, false , null );
// register to message received
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
// subscribe to the topic "/home/temperature" with QoS 2
client.Subscribe(new string[] { "hello/world" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
}
void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
Debug.Log("Received: " + System.Text.Encoding.UTF8.GetString(e.Message) );
tempDemo = int.Parse(System.Text.Encoding.UTF8.GetString(e.Message));
}
void OnGUI(){
if ( GUI.Button (new Rect (20,40,80,20), "Level 1")) {
Debug.Log("sending...");
client.Publish("hello/world", System.Text.Encoding.UTF8.GetBytes("bro"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
Debug.Log("sent");
}
}
// Update is called once per frame
void Update () {
}
}