Assets \ mqtthandler.cs(59,26):错误CS7069:对“ IPAddress”类型的引用声称它是在“系统”中定义的,但找不到它

时间:2019-06-20 16:01:19

标签: c# visual-studio unity3d hololens visual-studio-2019

我使用Unity中的M2MQTT库制作了MQTT客户端,用于Microsoft Hololens,但是我无法构建错误为“ Assets \ mqtthandler.cs(59,26):错误CS7069:引用为'IPAddress'的声明的Unity项目它在“系统”中定义,但找不到”。

我需要构建它才能将其部署到Hololens Emulator。

在我的脚本mqtthandler.cs中,我的代码中的brokerHostname在尝试创建MQTTClient时以某种方式引发此错误。仅当尝试在Unity中构建我的项目时,Visual Studio才在此脚本中看不到错误,并且在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 个答案:

答案 0 :(得分:1)

已解决的问题:我必须构建M2MQTT.WinRT.dll并将其也导入到Assets文件夹中。现在,我可以成功构建Unity项目了。