mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 11:45:43 +08:00
Expose custom session items in session status.
This commit is contained in:
@@ -25,6 +25,7 @@
|
|||||||
* [Server] Added support for assigned client IDs (MQTTv5 only) (thanks to @bcrosnier).
|
* [Server] Added support for assigned client IDs (MQTTv5 only) (thanks to @bcrosnier).
|
||||||
* [Server] Added interceptor for unsubscriptions.
|
* [Server] Added interceptor for unsubscriptions.
|
||||||
* [Server] Removed exceptions when user properties are set with MQTT protocol version 3.1
|
* [Server] Removed exceptions when user properties are set with MQTT protocol version 3.1
|
||||||
|
* [Server] Added custom session items to the client status.
|
||||||
* [MQTTnet.AspNetCore] improved compatibility with AspNetCore 3.1
|
* [MQTTnet.AspNetCore] improved compatibility with AspNetCore 3.1
|
||||||
* [MQTTnet.Server] Added interceptor for unsubscriptions.
|
* [MQTTnet.Server] Added interceptor for unsubscriptions.
|
||||||
</releaseNotes>
|
</releaseNotes>
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ namespace MQTTnet.Server
|
|||||||
{
|
{
|
||||||
public class MqttClientSession
|
public class MqttClientSession
|
||||||
{
|
{
|
||||||
private readonly IMqttNetLogger _logger;
|
readonly IMqttNetLogger _logger;
|
||||||
|
|
||||||
private readonly DateTime _createdTimestamp = DateTime.UtcNow;
|
readonly DateTime _createdTimestamp = DateTime.UtcNow;
|
||||||
|
|
||||||
public MqttClientSession(string clientId, IDictionary<object, object> items, MqttServerEventDispatcher eventDispatcher, IMqttServerOptions serverOptions, IMqttNetLogger logger)
|
public MqttClientSession(string clientId, IDictionary<object, object> items, MqttServerEventDispatcher eventDispatcher, IMqttServerOptions serverOptions, IMqttNetLogger logger)
|
||||||
{
|
{
|
||||||
@@ -73,6 +73,7 @@ namespace MQTTnet.Server
|
|||||||
status.ClientId = ClientId;
|
status.ClientId = ClientId;
|
||||||
status.CreatedTimestamp = _createdTimestamp;
|
status.CreatedTimestamp = _createdTimestamp;
|
||||||
status.PendingApplicationMessagesCount = ApplicationMessagesQueue.Count;
|
status.PendingApplicationMessagesCount = ApplicationMessagesQueue.Count;
|
||||||
|
status.Items = Items;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
using System;
|
using MQTTnet.Formatter;
|
||||||
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MQTTnet.Formatter;
|
|
||||||
|
|
||||||
namespace MQTTnet.Server.Status
|
namespace MQTTnet.Server.Status
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace MQTTnet.Server.Status
|
namespace MQTTnet.Server.Status
|
||||||
{
|
{
|
||||||
public interface IMqttSessionStatus
|
public interface IMqttSessionStatus
|
||||||
{
|
{
|
||||||
string ClientId { get; set; }
|
string ClientId { get; }
|
||||||
|
|
||||||
long PendingApplicationMessagesCount { get; set; }
|
long PendingApplicationMessagesCount { get; }
|
||||||
|
|
||||||
|
IDictionary<object, object> Items { get; }
|
||||||
|
|
||||||
Task ClearPendingApplicationMessagesAsync();
|
Task ClearPendingApplicationMessagesAsync();
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
using System;
|
using MQTTnet.Formatter;
|
||||||
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MQTTnet.Formatter;
|
|
||||||
|
|
||||||
namespace MQTTnet.Server.Status
|
namespace MQTTnet.Server.Status
|
||||||
{
|
{
|
||||||
public class MqttClientStatus : IMqttClientStatus
|
public class MqttClientStatus : IMqttClientStatus
|
||||||
{
|
{
|
||||||
private readonly MqttClientConnection _connection;
|
readonly MqttClientConnection _connection;
|
||||||
|
|
||||||
public MqttClientStatus(MqttClientConnection connection)
|
public MqttClientStatus(MqttClientConnection connection)
|
||||||
{
|
{
|
||||||
_connection = connection;
|
_connection = connection ?? throw new ArgumentNullException(nameof(connection));
|
||||||
}
|
}
|
||||||
|
|
||||||
public string ClientId { get; set; }
|
public string ClientId { get; set; }
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace MQTTnet.Server.Status
|
namespace MQTTnet.Server.Status
|
||||||
{
|
{
|
||||||
public class MqttSessionStatus : IMqttSessionStatus
|
public class MqttSessionStatus : IMqttSessionStatus
|
||||||
{
|
{
|
||||||
private readonly MqttClientSession _session;
|
readonly MqttClientSession _session;
|
||||||
private readonly MqttClientSessionsManager _sessionsManager;
|
readonly MqttClientSessionsManager _sessionsManager;
|
||||||
|
|
||||||
public MqttSessionStatus(MqttClientSession session, MqttClientSessionsManager sessionsManager)
|
public MqttSessionStatus(MqttClientSession session, MqttClientSessionsManager sessionsManager)
|
||||||
{
|
{
|
||||||
@@ -20,6 +21,8 @@ namespace MQTTnet.Server.Status
|
|||||||
|
|
||||||
public DateTime CreatedTimestamp { get; set; }
|
public DateTime CreatedTimestamp { get; set; }
|
||||||
|
|
||||||
|
public IDictionary<object, object> Items { get; set; }
|
||||||
|
|
||||||
public Task DeleteAsync()
|
public Task DeleteAsync()
|
||||||
{
|
{
|
||||||
return _sessionsManager.DeleteSessionAsync(ClientId);
|
return _sessionsManager.DeleteSessionAsync(ClientId);
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
using System.Text;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using MQTTnet.Client;
|
using MQTTnet.Client;
|
||||||
using MQTTnet.Client.Subscribing;
|
using MQTTnet.Client.Subscribing;
|
||||||
using MQTTnet.Server;
|
using MQTTnet.Server;
|
||||||
using MQTTnet.Tests.Mockups;
|
using MQTTnet.Tests.Mockups;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace MQTTnet.Tests
|
namespace MQTTnet.Tests
|
||||||
{
|
{
|
||||||
@@ -42,7 +43,7 @@ namespace MQTTnet.Tests
|
|||||||
string receivedPayload = null;
|
string receivedPayload = null;
|
||||||
|
|
||||||
var client = await testEnvironment.ConnectClientAsync();
|
var client = await testEnvironment.ConnectClientAsync();
|
||||||
client.UseApplicationMessageReceivedHandler(delegate(MqttApplicationMessageReceivedEventArgs args)
|
client.UseApplicationMessageReceivedHandler(delegate (MqttApplicationMessageReceivedEventArgs args)
|
||||||
{
|
{
|
||||||
receivedPayload = args.ApplicationMessage.ConvertPayloadToString();
|
receivedPayload = args.ApplicationMessage.ConvertPayloadToString();
|
||||||
});
|
});
|
||||||
@@ -59,5 +60,29 @@ namespace MQTTnet.Tests
|
|||||||
Assert.AreEqual("Hello World", receivedPayload);
|
Assert.AreEqual("Hello World", receivedPayload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task Get_Session_Items_In_Status()
|
||||||
|
{
|
||||||
|
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||||
|
{
|
||||||
|
var serverOptions = new MqttServerOptionsBuilder()
|
||||||
|
.WithConnectionValidator(delegate (MqttConnectionValidatorContext context)
|
||||||
|
{
|
||||||
|
// Don't validate anything. Just set some session items.
|
||||||
|
context.SessionItems["can_subscribe_x"] = true;
|
||||||
|
context.SessionItems["default_payload"] = "Hello World";
|
||||||
|
});
|
||||||
|
|
||||||
|
await testEnvironment.StartServerAsync(serverOptions);
|
||||||
|
|
||||||
|
var client = await testEnvironment.ConnectClientAsync();
|
||||||
|
|
||||||
|
var sessionStatus = await testEnvironment.Server.GetSessionStatusAsync();
|
||||||
|
var session = sessionStatus.First();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, session.Items["can_subscribe_x"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user