|
|
@ -6,6 +6,7 @@ using MQTTnet.Client;
|
|
|
|
using System.Security.Authentication;
|
|
|
|
using System.Security.Authentication;
|
|
|
|
using MQTTnet;
|
|
|
|
using MQTTnet;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
using SlnMesnac.Serilog;
|
|
|
|
|
|
|
|
|
|
|
|
#region << 版 本 注 释 >>
|
|
|
|
#region << 版 本 注 释 >>
|
|
|
|
/*--------------------------------------------------------------------
|
|
|
|
/*--------------------------------------------------------------------
|
|
|
@ -36,11 +37,11 @@ namespace SlnMesnac.Mqtt
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
public class MqttClient
|
|
|
|
public class MqttClient
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private ILogger<MqttClient> _logger;
|
|
|
|
public readonly SerilogHelper _logger;
|
|
|
|
|
|
|
|
|
|
|
|
private IMqttClient _client;
|
|
|
|
private IMqttClient _client;
|
|
|
|
|
|
|
|
|
|
|
|
public MqttClient(ILogger<MqttClient> logger)
|
|
|
|
public MqttClient(SerilogHelper logger)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -78,17 +79,17 @@ namespace SlnMesnac.Mqtt
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (result.ResultCode == MQTTnet.Client.MqttClientConnectResultCode.Success)
|
|
|
|
if (result.ResultCode == MQTTnet.Client.MqttClientConnectResultCode.Success)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.LogInformation($"连接服务器成功{ip}:{port}");
|
|
|
|
_logger.Info($"连接服务器成功{ip}:{port}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.LogInformation($"连接服务器失败");
|
|
|
|
_logger.Info($"连接服务器失败");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.LogError("连接服务器异常",ex);
|
|
|
|
_logger.Error("连接服务器异常",ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -98,7 +99,7 @@ namespace SlnMesnac.Mqtt
|
|
|
|
public void DisConnect()
|
|
|
|
public void DisConnect()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_client.DisconnectAsync();
|
|
|
|
_client.DisconnectAsync();
|
|
|
|
_logger.LogInformation($"断开连接");
|
|
|
|
_logger.Info($"断开连接");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
@ -120,11 +121,11 @@ namespace SlnMesnac.Mqtt
|
|
|
|
|
|
|
|
|
|
|
|
MqttClientSubscribeResult result = await _client.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
|
|
|
|
MqttClientSubscribeResult result = await _client.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation($"订阅主题:{topic}");
|
|
|
|
_logger.Info($"订阅主题:{topic}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.LogError("订阅主题异常",ex);
|
|
|
|
_logger.Error("订阅主题异常",ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -135,7 +136,7 @@ namespace SlnMesnac.Mqtt
|
|
|
|
public void Unsubscribe(string topic)
|
|
|
|
public void Unsubscribe(string topic)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_client.UnsubscribeAsync(topic);
|
|
|
|
_client.UnsubscribeAsync(topic);
|
|
|
|
_logger.LogInformation($"取消订阅,主题:{topic}");
|
|
|
|
_logger.Info($"取消订阅,主题:{topic}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
@ -150,18 +151,18 @@ namespace SlnMesnac.Mqtt
|
|
|
|
var msg = new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(message)
|
|
|
|
var msg = new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(message)
|
|
|
|
.Build();
|
|
|
|
.Build();
|
|
|
|
_client.PublishAsync(msg, CancellationToken.None);
|
|
|
|
_client.PublishAsync(msg, CancellationToken.None);
|
|
|
|
_logger.LogInformation($"向服务端推送成功,主题:{topic};内容:{message}");
|
|
|
|
_logger.Info($"向服务端推送成功,主题:{topic};内容:{message}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.LogError("向服务端推送消息异常",ex);
|
|
|
|
_logger.Error("向服务端推送消息异常",ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task MqttClient_ApplicationMessageReceived(MqttApplicationMessageReceivedEventArgs eventArgs)
|
|
|
|
private async Task MqttClient_ApplicationMessageReceived(MqttApplicationMessageReceivedEventArgs eventArgs)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var info = $"接收到主题:{eventArgs.ApplicationMessage.Topic}的消息,内容:{Encoding.UTF8.GetString(eventArgs.ApplicationMessage.Payload)}";
|
|
|
|
var info = $"接收到主题:{eventArgs.ApplicationMessage.Topic}的消息,内容:{Encoding.UTF8.GetString(eventArgs.ApplicationMessage.Payload)}";
|
|
|
|
_logger.LogInformation(info);
|
|
|
|
_logger.Info(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|