Assets \ mqtthandler.cs (59,26): ошибка CS7069: ссылка на тип 'IPAddress' утверждает, что она определена в 'System', но не может быть найдена - PullRequest
0 голосов
/ 20 июня 2019

Я создал MQTT-клиент с помощью библиотеки M2MQTT в Unity для Microsoft Hololens, но не могу создать проект Unity с ошибкой «Assets \ mqtthandler.cs (59,26): ошибка CS7069: Ссылка на утверждения типа« IP-адрес » он определен в «Системе», но не может быть найден ».

Мне нужно собрать его, чтобы развернуть в эмуляторе Хололенса.

В моем скрипте mqtthandler.cs brokerHostname в моем коде как-то вызывает эту ошибку при попытке создать MQTTClient. Visual Studio не видит ошибок в этом сценарии, только при попытке построить мой проект в Unity я получаю эту ошибку в консоли Unity.

Если я запускаю свой проект в самом Unity, я могу успешно получать mqtt-сообщения.

Я уже переимпортировал все ресурсы, очистил кеш Unity, восстановил M2MQTT.NET.dll.

Я использую Unity 2018.3 и Visual Studio 2019.

Мой код:

using System.Collections;
using System.Collections.Generic;
using System.Text;
using System;
using UnityEngine;


// including the M2Mqtt Library
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using uPLibrary.Networking.M2Mqtt.Exceptions;

using System.Net;
using System.Net.Sockets;



public class mqtthandler : MonoBehaviour
{
    private MqttClient client;
    // The connection information
    public string brokerHostname = "127.0.0.1";
    public int brokerPort = 1883;
    public string userName = "test";
    public string password = "test";
    public static string messageOutput;
    public static string topicOutput;
    //public TextAsset certificate;
    // listen on all the Topic
    static string subTopic = "#";

    // Start is called before the first frame update
    void Start()
    {
        if (brokerHostname != null && userName != null && password !=null)
        {
            Debug.Log("connecting to " + brokerHostname + ":" + brokerPort);
            Connect();
            client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
            byte[] qosLevels = { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE };
            client.Subscribe(new string[] { subTopic }, qosLevels);
        }
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void Connect()
    {
        Debug.Log("about to connect on '" + brokerHostname + "'");
        // Forming a certificate based on a TextAsset
        /*X509Certificate cert = new X509Certificate();
        cert.Import(certificate.bytes);
        Debug.Log("Using the certificate '" + cert + "'");*/
        client = new MqttClient(brokerHostname/*, brokerPort, false, null, true, cert, null, MqttSslProtocols.TLSv1_0, MyRemoteCertificateValidationCallback*/);
        string clientId = Guid.NewGuid().ToString();
        Debug.Log("About to connect using '" + userName + "' / '" + password + "'");
        try
        {
            client.Connect(clientId, userName, password);
        }
        catch (Exception e)
        {
            Debug.LogError("Connection error: " + e);
        }
    }

    void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
    {
        string msg = System.Text.Encoding.UTF8.GetString(e.Message);
        Debug.Log("Received message from " + e.Topic + " : " + msg);
        messageOutput = msg;
        topicOutput = e.Topic;
    }

    private void Publish(string _topic, string msg)
    {
        client.Publish(
            _topic, Encoding.UTF8.GetBytes(msg),
            MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false);
    }
}

1 Ответ

1 голос
/ 22 июня 2019

Проблема решена: мне пришлось собрать M2MQTT.WinRT.dll и импортировать его также в папку Assets.Теперь я могу успешно построить проект Unity.

...