Comments (5)
Thanks for the help, I added WithDefaultEndpoint () and it works fine
from mqttnet.
I would recommend to build a background service following https://learn.microsoft.com/en-us/dotnet/core/extensions/windows-service and provide it within the startup:
service.AddSingleton<MyBackgroundService>();
service.AddHostedService(p => p.GetRequiredService<MyBackgroundService>());
from mqttnet.
I would recommend to build a background service following https://learn.microsoft.com/en-us/dotnet/core/extensions/windows-service and provide it within the startup:
service.AddSingleton<MyBackgroundService>(); service.AddHostedService(p => p.GetRequiredService<MyBackgroundService>());
Hello, test code output: StartAsync still does not open the port, IMqttNetLogger log has output: Started, and then there is no active content output.
public class IotMqttServer : IHostedService
{
private MqttServer mMqtt;
public IotMqttServer()
{
}
public async Task StartAsync(CancellationToken cancellationToken)
{
try
{
var optionsBuilder = new MqttServerOptionsBuilder().WithConnectionBacklog(100).WithDefaultEndpointPort(87941);
var mMqtt = new MqttFactory(new MyLogger()).CreateMqttServer(optionsBuilder.Build());
await mMqtt.StartAsync();
System.Diagnostics.Debug.WriteLine("StartAsync");
}
catch (Exception)
{
}
}
public async Task StopAsync(CancellationToken cancellationToken)
{
if (mMqtt != null)
{
if (mMqtt.IsStarted)
{
await mMqtt.StopAsync();
}
}
}}
builder.Services.AddSingleton<IotMqttServer>();
builder.Services.AddHostedService(P=>P.GetRequiredService<IotMqttServer>());
from mqttnet.
@shouhujishu This is how I use it:
using MQTTnet;
using MQTTnet.Protocol;
using MQTTnet.Server;
/// <inheritdoc cref="BackgroundService"/>
public sealed class MqttBrokerService : BackgroundService
{
/// <summary>
/// The MQTT Server.
/// </summary>
public MqttServer MqttServer { get; internal set; }
/// <summary>
/// Initializes a new instance of the <see cref="MqttBrokerService"/> class.
/// </summary>
public MqttBrokerService()
{
var mqttFactory = new MqttFactory();
var mqttServerOptions = mqttFactory
.CreateServerOptionsBuilder()
.WithDefaultEndpoint()
.WithDefaultEndpointBoundIPAddress(IPAddress.Parse("0.0.0.0"))
.WithDefaultEndpointPort(1883)
.Build();
this.MqttServer = mqttFactory.CreateMqttServer(mqttServerOptions);
}
/// <inheritdoc cref="BackgroundService"/>
public override async Task StartAsync(CancellationToken cancellationToken)
{
this.logger.Information("Starting MQTT server on port {Port}", 1883);
// Start the MQTT server
await this.MqttServer.StartAsync();
this.logger.Information("MQTT server has started");
await base.StartAsync(cancellationToken);
}
/// <inheritdoc cref="BackgroundService"/>
public override async Task StopAsync(CancellationToken cancellationToken)
{
this.logger.Information("Stopping MQTT server");
await this.MqttServer.StopAsync();
await base.StopAsync(cancellationToken);
this.logger.Information("MQTT server is stopped");
}
/// <inheritdoc cref="BackgroundService"/>
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(TimeSpan.FromSeconds(30), stoppingToken);
this.logger.Information("MQTT server is running");
}
}
}
I guess, you were missing .WithDefaultEndpoint()
and .WithDefaultEndpointBoundIPAddress(IPAddress.Parse("0.0.0.0"))
. Note: 0.0.0.0
as IP address is needed if you want to have the broker accessible from outside the machine as well, else you can use 127.0.0.1
as well. You can change the port of 1883
to any other port, of course.
from mqttnet.
I guess, we can close this :)
from mqttnet.
Related Issues (20)
- Connection timeout error due to high latency on the network
- Invert conditions in aspnet package csproj
- Cannot Connect Server on Android/MAUI Blazor Project
- Maximum message / s throughput for a single subscriber can't stay above 10000 m/s HOT 5
- Flaky exceptions for oversized messages HOT 1
- Intercepting Publish on server does not return the correct status code to the client HOT 3
- Retained Messages limited to 250 HOT 1
- MqttClient Publish a message when you are inside the `InterceptingPublishAsync` result to an Error when the Client Publish a QoS 1
- MqttServer messages send get lost while client is subscribing HOT 2
- Unable to use certificate in Ubuntu (TLS1.2) HOT 1
- MqttClient with TLS and client certificate works on Linux but not on windows using the latest and following samples HOT 18
- No valid replacement for the obsolete MqttClientOptionsBuilder.WithConnectionUri HOT 1
- MQTTnet.AspTestApp Fails to start HOT 1
- MQTTnet.Extensions.WebSocket4Net uses several outdated and possibly vulnerable dependencies HOT 3
- MQTT-over-Websocket connection reconnects every 30 seconds (MAUI regression) HOT 1
- Can't connect to broker using CA Cert ClientCert and Key HOT 2
- Unable to connect with Unix Socket HOT 4
- Last Will not working? HOT 2
- [FR] Protobuf HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mqttnet.