mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-18 18:43:10 +08:00
Fix client dead lock and protocol version issues.
This commit is contained in:
@@ -13,7 +13,7 @@ namespace MQTTnet.Core.Tests
|
||||
_adapter = adapter;
|
||||
}
|
||||
|
||||
public IMqttChannelAdapter CreateClientAdapter(IMqttClientChannelOptions options, IMqttNetLogger logger)
|
||||
public IMqttChannelAdapter CreateClientAdapter(IMqttClientOptions options, IMqttNetLogger logger)
|
||||
{
|
||||
return _adapter;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Server;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
@@ -38,6 +37,12 @@ namespace MQTTnet.TestApp.NetCore
|
||||
// based payload with the timestamp is a suitable use case.
|
||||
context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O"));
|
||||
}
|
||||
|
||||
if (context.ApplicationMessage.Topic == "not_allowed_topic")
|
||||
{
|
||||
context.AcceptPublish = false;
|
||||
context.CloseConnection = true;
|
||||
}
|
||||
},
|
||||
SubscriptionInterceptor = context =>
|
||||
{
|
||||
@@ -72,27 +77,27 @@ namespace MQTTnet.TestApp.NetCore
|
||||
ConsoleColor.Magenta);
|
||||
};
|
||||
|
||||
options.ApplicationMessageInterceptor = c =>
|
||||
{
|
||||
if (c.ApplicationMessage.Payload == null || c.ApplicationMessage.Payload.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
//options.ApplicationMessageInterceptor = c =>
|
||||
//{
|
||||
// if (c.ApplicationMessage.Payload == null || c.ApplicationMessage.Payload.Length == 0)
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
|
||||
try
|
||||
{
|
||||
var content = JObject.Parse(Encoding.UTF8.GetString(c.ApplicationMessage.Payload));
|
||||
var timestampProperty = content.Property("timestamp");
|
||||
if (timestampProperty != null && timestampProperty.Value.Type == JTokenType.Null)
|
||||
{
|
||||
timestampProperty.Value = DateTime.Now.ToString("O");
|
||||
c.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(content.ToString());
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
};
|
||||
// try
|
||||
// {
|
||||
// var content = JObject.Parse(Encoding.UTF8.GetString(c.ApplicationMessage.Payload));
|
||||
// var timestampProperty = content.Property("timestamp");
|
||||
// if (timestampProperty != null && timestampProperty.Value.Type == JTokenType.Null)
|
||||
// {
|
||||
// timestampProperty.Value = DateTime.Now.ToString("O");
|
||||
// c.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(content.ToString());
|
||||
// }
|
||||
// }
|
||||
// catch (Exception)
|
||||
// {
|
||||
// }
|
||||
//};
|
||||
|
||||
mqttServer.ClientDisconnected += (s, e) =>
|
||||
{
|
||||
|
||||
@@ -13,6 +13,8 @@ using MQTTnet.Implementations;
|
||||
using MQTTnet.ManagedClient;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Server;
|
||||
using MqttClientConnectedEventArgs = MQTTnet.Client.MqttClientConnectedEventArgs;
|
||||
using MqttClientDisconnectedEventArgs = MQTTnet.Client.MqttClientDisconnectedEventArgs;
|
||||
|
||||
namespace MQTTnet.TestApp.UniversalWindows
|
||||
{
|
||||
@@ -33,6 +35,11 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
private async void OnTraceMessagePublished(object sender, MqttNetLogMessagePublishedEventArgs e)
|
||||
{
|
||||
_traceMessages.Enqueue(e.TraceMessage);
|
||||
await UpdateLogAsync();
|
||||
}
|
||||
|
||||
private async Task UpdateLogAsync()
|
||||
{
|
||||
while (_traceMessages.Count > 100)
|
||||
{
|
||||
_traceMessages.TryDequeue(out _);
|
||||
@@ -113,11 +120,15 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
{
|
||||
await _mqttClient.DisconnectAsync();
|
||||
_mqttClient.ApplicationMessageReceived -= OnApplicationMessageReceived;
|
||||
_mqttClient.Connected -= OnConnected;
|
||||
_mqttClient.Disconnected -= OnDisconnected;
|
||||
}
|
||||
|
||||
var factory = new MqttFactory();
|
||||
_mqttClient = factory.CreateMqttClient();
|
||||
_mqttClient.ApplicationMessageReceived += OnApplicationMessageReceived;
|
||||
_mqttClient.Connected += OnConnected;
|
||||
_mqttClient.Disconnected += OnDisconnected;
|
||||
|
||||
await _mqttClient.ConnectAsync(options);
|
||||
}
|
||||
@@ -127,6 +138,22 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDisconnected(object sender, MqttClientDisconnectedEventArgs e)
|
||||
{
|
||||
_traceMessages.Enqueue(new MqttNetLogMessage("", DateTime.Now, -1,
|
||||
"", MqttNetLogLevel.Info, "! DISCONNECTED EVENT FIRED", null));
|
||||
|
||||
Task.Run(UpdateLogAsync);
|
||||
}
|
||||
|
||||
private void OnConnected(object sender, MqttClientConnectedEventArgs e)
|
||||
{
|
||||
_traceMessages.Enqueue(new MqttNetLogMessage("", DateTime.Now, -1,
|
||||
"", MqttNetLogLevel.Info, "! CONNECTED EVENT FIRED", null));
|
||||
|
||||
Task.Run(UpdateLogAsync);
|
||||
}
|
||||
|
||||
private async void OnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs eventArgs)
|
||||
{
|
||||
var item = $"Timestamp: {DateTime.Now:O} | Topic: {eventArgs.ApplicationMessage.Topic} | Payload: {Encoding.UTF8.GetString(eventArgs.ApplicationMessage.Payload)} | QoS: {eventArgs.ApplicationMessage.QualityOfServiceLevel}";
|
||||
|
||||
Reference in New Issue
Block a user