mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 09:13:02 +08:00
Add Test-MqttConnection and Show-MqttExamples cmdlets, update documentation
Co-authored-by: chkr1011 <6939810+chkr1011@users.noreply.github.com>
This commit is contained in:
co-authored by
chkr1011
parent
07b72e75ed
commit
16f8765562
@@ -0,0 +1,41 @@
|
||||
// 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 System.Management.Automation;
|
||||
|
||||
namespace MQTTnet.PowerShell.Cmdlets;
|
||||
|
||||
[Cmdlet(VerbsDiagnostic.Test, "MqttConnection")]
|
||||
[OutputType(typeof(bool))]
|
||||
public class TestMqttConnectionCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter(Mandatory = true, ValueFromPipeline = true)]
|
||||
public required PsMqttSession Session { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public int TimeoutSeconds { get; set; } = 5;
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
try
|
||||
{
|
||||
var client = Session.GetClient();
|
||||
|
||||
if (!client.IsConnected)
|
||||
{
|
||||
WriteObject(false);
|
||||
return;
|
||||
}
|
||||
|
||||
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(TimeoutSeconds));
|
||||
client.PingAsync(cts.Token).GetAwaiter().GetResult();
|
||||
|
||||
WriteObject(true);
|
||||
}
|
||||
catch
|
||||
{
|
||||
WriteObject(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user