forked from dotnet/MQTTnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some extension methods to IMqttChannelAdapter.
- Loading branch information
Showing
5 changed files
with
80 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Source/MQTTnet.AspnetCore/Internal/IAspNetCoreMqttChannelAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using MQTTnet.Adapter; | ||
|
||
namespace MQTTnet.AspNetCore | ||
{ | ||
interface IAspNetCoreMqttChannelAdapter : IMqttChannelAdapter | ||
{ | ||
HttpContext? HttpContext { get; } | ||
IFeatureCollection? Features { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.AspNetCore.Http; | ||
using MQTTnet.Adapter; | ||
using System; | ||
|
||
namespace MQTTnet.AspNetCore | ||
{ | ||
public static class MqttChannelAdapterExtensions | ||
{ | ||
public static bool? IsWebSocketConnection(this IMqttChannelAdapter channelAdapter) | ||
{ | ||
ArgumentNullException.ThrowIfNull(channelAdapter); | ||
return channelAdapter is IAspNetCoreMqttChannelAdapter adapter | ||
? adapter.Features != null && adapter.Features.Get<WebSocketConnectionFeature>() != null | ||
: null; | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves the requested feature from the feature collection of channelAdapter. | ||
/// </summary> | ||
/// <typeparam name="TFeature"></typeparam> | ||
/// <param name="channelAdapter"></param> | ||
/// <returns></returns> | ||
public static TFeature? GetFeature<TFeature>(this IMqttChannelAdapter channelAdapter) | ||
{ | ||
ArgumentNullException.ThrowIfNull(channelAdapter); | ||
return channelAdapter is IAspNetCoreMqttChannelAdapter adapter && adapter.Features != null | ||
? adapter.Features.Get<TFeature>() | ||
: default; | ||
} | ||
|
||
/// <summary> | ||
/// When the channelAdapter is a WebSocket connection, it can get an associated <see cref="HttpContext"/>. | ||
/// </summary> | ||
/// <param name="channelAdapter"></param> | ||
/// <returns></returns> | ||
public static HttpContext? GetHttpContext(this IMqttChannelAdapter channelAdapter) | ||
{ | ||
ArgumentNullException.ThrowIfNull(channelAdapter); | ||
return channelAdapter is IAspNetCoreMqttChannelAdapter adapter | ||
? adapter.HttpContext | ||
: null; | ||
} | ||
} | ||
} |