我已经在下面构建了此错误

问题描述 投票:0回答:1
The type or namespace name 'Options' does not exist in the namespace 'MQTTnet.Client' (are you missing an assembly reference?) The type or namespace name 'Subscribing' does not exist in the namespace 'MQTTnet.Client' (are you missing an assembly reference?)

在我的测试中,我发现您的项目中有3个构建错误。 首先,请将mqttfactory()更改为mqttclientfactory()。
 var mqttClient = new MqttClientFactory().CreateMqttClient();

秒,请使用扩展方法在ApplicationMessageReceivedasync中显示输出,因为无法访问E.ApplicationMessage。

延伸方法:(来自
Github
c# visual-studio mqtt subscriber mqttnet
1个回答
0
投票
internal static class ObjectExtensions { public static TObject DumpToConsole<TObject>(this TObject @object) { var output = "NULL"; if (@object != null) { output = JsonSerializer.Serialize(@object, new JsonSerializerOptions { WriteIndented = true }); } Console.WriteLine($"[{@object?.GetType().Name}]:\r\n{output}"); return @object; } }

deletegate用法:

// Set up the handler for receiving messages mqttClient.ApplicationMessageReceivedAsync += e => { Console.WriteLine("Received application message."); e.DumpToConsole(); return Task.CompletedTask; };

最终,请删除与MQTTNET相关的所有引用,然后安装Nuget-packagemqttnet.

here是一个完整的代码示例,没有构建错误。
using MQTTnet;
using System.Text.Json;

namespace OtTtoIT11
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            string brokerAddress = "0.0.0.0";  // Your MQTT broker IP address
            int brokerPort = 1883;  // Default MQTT port
            string topic = "#";  // Your MQTT topic

            // Create a new MQTT client
            var mqttClient = new MqttClientFactory().CreateMqttClient();

            // Create client options using the MqttClientOptionsBuilder
            var mqttClientOptions = new MqttClientOptionsBuilder()
                .WithClientId("YourClientId")
                .WithTcpServer(brokerAddress, brokerPort)
                .WithCleanSession()
                .Build();

            // Set up the handler for receiving messages
            mqttClient.ApplicationMessageReceivedAsync += e =>
            {
                Console.WriteLine("Received application message.");
                e.DumpToConsole();

                return Task.CompletedTask;
                
            };

            // Connect to the MQTT broker
            await mqttClient.ConnectAsync(mqttClientOptions);
            Console.WriteLine("Connected to the broker.");

            // Subscribe to the topic
            var subscribeOptions = new MqttClientSubscribeOptionsBuilder()
                .WithTopicFilter(f => { f.WithTopic(topic); })
                .Build();

            await mqttClient.SubscribeAsync(subscribeOptions);
            Console.WriteLine($"Subscribed to topic: {topic}");

            // Keep the application running to receive messages
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();

            // Disconnect the client
            await mqttClient.DisconnectAsync();
            Console.WriteLine("Disconnected from the broker.");
        }
    }

    internal static class ObjectExtensions
    {
        public static TObject DumpToConsole<TObject>(this TObject @object)
        {
            var output = "NULL";
            if (@object != null)
            {
                output = JsonSerializer.Serialize(@object, new JsonSerializerOptions
                {
                    WriteIndented = true
                });
            }

            Console.WriteLine($"[{@object?.GetType().Name}]:\r\n{output}");
            return @object;
        }
    }
}

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.