ttpsan550 2023-06-07 20:52 采纳率: 100%
浏览 66
已结题

关于aspnetcore中使用mqttnet库的entire

关于 MqttNet 库的使用问题,在github上 [https://github.com/dotnet/MQTTnet]下载源码,运行里面的 MQTTnet.AspTestApp 项目,
program

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using MQTTnet;
using MQTTnet.AspNetCore;
using MQTTnet.Server;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();

// Setup MQTT stuff.
builder.Services.AddMqttServer(opt=>
{
    opt.WithDefaultEndpointPort(1883);                           //mqttServerOptionsBuilder.WithEncryptedEndpointPort(port);                        // 使用加密的端点端口
    opt.WithPersistentSessions(true);  // 持续会话
    opt.WithConnectionBacklog(2000);
});
builder.Services.AddConnections();
var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
}

app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

// Setup MQTT stuff.
app.UseEndpoints(endpoints =>
{
    endpoints.MapMqtt("/mqtt");
});

app.UseMqttServer(server =>
{
    
    server.StartedAsync += args =>
    {
        _ = Task.Run(async () =>
        {
            var mqttApplicationMessage = new MqttApplicationMessageBuilder()
                .WithPayload($"Test application message from MQTTnet server.")
                .WithTopic("message")
                .Build();

            while (true)
            {
                try
                {
                    await server.InjectApplicationMessage(new InjectedMqttApplicationMessage(mqttApplicationMessage)
                    {
                        SenderClientId = "server"
                    });
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
                finally
                {
                    await Task.Delay(TimeSpan.FromSeconds(5));
                }
            }
        });

        return Task.CompletedTask;
    };
});

app.Run();


基本上的官方源码例程, 客户端连接不起来,不知道是不是哪里没有配置好

img

  • 写回答

2条回答 默认 最新

  • wanghui0380 2023-06-08 09:17
    关注

    你的连接里面,官方的demo代码并不长成你贴的模样
    下面是官方的测试demo代码
    https://github.com/dotnet/MQTTnet/blob/master/Samples/Server/Server_ASP_NET_Samples.cs

    代码核心是“需要告诉aspnet core启动时同时监听1883端口”------这个其实是对tcp端的处理
    当然你可以直接用“ws://localhost:9914/mqtt”进行连接,这个才是ws的连接路径

    ps:mqtt通常默认端口
    tcp:1883
    ws:8083
    ssl tcp:1884,
    wss:8084

    所以你看得到他们是分开监听的,所以按你目前的代码,其实就是ws:9914

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月16日
  • 已采纳回答 6月8日
  • 赞助了问题酬金15元 6月7日
  • 创建了问题 6月7日

悬赏问题

  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活
  • ¥15 sqlserver中加密的密码字段查询问题
  • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
  • ¥20 我的这个coe文件到底哪儿出问题了
  • ¥15 matlab使用自定义函数时一直报错输入参数过多
  • ¥15 设计一个温度闭环控制系统
  • ¥100 rtmpose姿态评估