Skip to content

Commit 3f45daf

Browse files
committed
增加 获取客户端和 sess 的方法
1 parent 29c84af commit 3f45daf

File tree

4 files changed

+24
-49
lines changed

4 files changed

+24
-49
lines changed

ExampleClient/ExampleClient.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@
99
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="4.0.1.184" />
1010
</ItemGroup>
1111

12+
<ItemGroup>
13+
<ProjectReference Include="..\Source\MQTTnet.AspNetCore.Routing.csproj" />
14+
</ItemGroup>
15+
1216
</Project>

ExampleServer/ExampleServer.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<ProjectReference Include="..\Source\MQTTnet.AspNetCore.AttributeRouting.csproj" />
8+
<ProjectReference Include="..\Source\MQTTnet.AspNetCore.Routing.csproj" />
99
</ItemGroup>
1010

1111

Source/MQTTnet.AspNetCore.AttributeRouting.csproj

-48
This file was deleted.

Source/Routing/MqttBaseController.cs

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using MQTTnet.AspNetCore.AttributeRouting.Attributes;
55
using MQTTnet.AspNetCore.AttributeRouting.Routing;
66
using MQTTnet.Server;
7+
using System.Linq;
78
using System.Threading.Tasks;
89

910
namespace MQTTnet.AspNetCore.AttributeRouting
@@ -26,6 +27,24 @@ public abstract class MqttBaseController
2627
/// </summary>
2728
public MqttServer Server => ControllerContext.MqttServer;
2829

30+
public string ClientId => MqttContext.ClientId;
31+
public async Task<MqttClientStatus> GetClientStatusAsync()
32+
{
33+
var clients = await Server.GetClientsAsync();
34+
return clients.FirstOrDefault(c => c.Id == MqttContext.ClientId);
35+
}
36+
public async Task<MqttSessionStatus> GetSessionAsync()
37+
{
38+
var client = await GetClientStatusAsync();
39+
return client?.Session;
40+
}
41+
42+
public async Task<T> GetSessionDataAsync<T>(string key)
43+
{
44+
var client = await GetClientStatusAsync();
45+
return (T)(client?.Session.Items[key]);
46+
}
47+
2948
/// <summary>
3049
/// ControllerContext is set by controller activator. If this class is instantiated directly, it will be null.
3150
/// </summary>

0 commit comments

Comments
 (0)