service
builder.Services.AddMqttServer(configure =>
{
var options = configure.WithDefaultEndpoint().Build();
var mqttFactory = new MqttFactory(new MqttConsoleLogger());
var mqttServer = mqttFactory.CreateMqttServer(options);
mqttServer.StartAsync().Wait();
mqttServer.InterceptingPublishAsync += InterceptApplicationMessagePublishAsync;
});
Task InterceptApplicationMessagePublishAsync(InterceptingPublishEventArgs args)
{
try
{
var message = args.ApplicationMessage;
var json = message.ConvertPayloadToString();
Console.WriteLine($"Topic:{message.Topic}, TopicAlias:{message.TopicAlias}, ResponseTopic:{message.ResponseTopic}, Message:{json}");
return Task.CompletedTask;
}
catch (Exception ex)
{
return Task.FromException(ex);
}
}
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly MqttServer _mqtt;
public HomeController(ILogger<HomeController> logger, MqttServer mqtt)
{
_logger = logger;
_mqtt = mqtt;
}
public async Task<IActionResult> GetClients()
{
var list = await _mqtt.GetClientsAsync();
return Json(list);
}
public async Task<IActionResult> Publish()
{
string topic = $"topictest";
string inputMessage = $"Test application message from MQTTnet server.";
MqttApplicationMessage application_message = new MqttApplicationMessageBuilder()
.WithPayload(inputMessage)
.WithTopic(topic)
.Build();
var message = new InjectedMqttApplicationMessage(application_message);
await _mqtt.InjectApplicationMessage(message);
return Json(new { DateTime = DateTime.Now, Guid = System.Guid.NewGuid().ToString() });
}
}
client
var mqttFactory = new MqttFactory();
using (var mqttClient = mqttFactory.CreateMqttClient())
{
var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").Build();
mqttClient.ApplicationMessageReceivedAsync += args =>
{
Console.WriteLine(args.ApplicationMessage.ConvertPayloadToString());
return Task.CompletedTask;
};
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder()
.WithTopicFilter(f => { f.WithTopic("topictest"); })
.Build();
var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
Console.WriteLine("MQTT client subscribed to topic.");
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
Where is my problem?
service
builder.Services.AddMqttServer(configure =>
{
var options = configure.WithDefaultEndpoint().Build();
var mqttFactory = new MqttFactory(new MqttConsoleLogger());
var mqttServer = mqttFactory.CreateMqttServer(options);
mqttServer.StartAsync().Wait();
mqttServer.InterceptingPublishAsync += InterceptApplicationMessagePublishAsync;
});
Task InterceptApplicationMessagePublishAsync(InterceptingPublishEventArgs args)
{
try
{
var message = args.ApplicationMessage;
var json = message.ConvertPayloadToString();
}
}
client
var mqttFactory = new MqttFactory();
using (var mqttClient = mqttFactory.CreateMqttClient())
{
var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").Build();
}
Where is my problem?