mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 01:03:01 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c0c5f20c5a |
@@ -10,7 +10,7 @@ using System.Security.Authentication;
|
|||||||
using MQTTnet.Client;
|
using MQTTnet.Client;
|
||||||
using MQTTnet.Extensions.WebSocket4Net;
|
using MQTTnet.Extensions.WebSocket4Net;
|
||||||
using MQTTnet.Formatter;
|
using MQTTnet.Formatter;
|
||||||
using MQTTnet.Samples.Helpers;
|
using MQTTnet.Samples.Shared;
|
||||||
|
|
||||||
namespace MQTTnet.Samples.Client;
|
namespace MQTTnet.Samples.Client;
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
// ReSharper disable InconsistentNaming
|
// ReSharper disable InconsistentNaming
|
||||||
|
|
||||||
using MQTTnet.Client;
|
using MQTTnet.Client;
|
||||||
using MQTTnet.Samples.Helpers;
|
using MQTTnet.Samples.Shared;
|
||||||
|
|
||||||
namespace MQTTnet.Samples.Client;
|
namespace MQTTnet.Samples.Client;
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using MQTTnet.Diagnostics;
|
using MQTTnet.Diagnostics;
|
||||||
|
using MQTTnet.Samples.Shared;
|
||||||
|
|
||||||
namespace MQTTnet.Samples.Diagnostics;
|
namespace MQTTnet.Samples.Diagnostics;
|
||||||
|
|
||||||
@@ -22,9 +23,7 @@ public static class Logger_Samples
|
|||||||
|
|
||||||
var mqttFactory = new MqttFactory(new MyLogger());
|
var mqttFactory = new MqttFactory(new MyLogger());
|
||||||
|
|
||||||
var mqttClientOptions = mqttFactory.CreateClientOptionsBuilder()
|
var mqttClientOptions = mqttFactory.CreateClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build();
|
||||||
.WithTcpServer("broker.hivemq.com")
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
using (var mqttClient = mqttFactory.CreateMqttClient())
|
using (var mqttClient = mqttFactory.CreateMqttClient())
|
||||||
{
|
{
|
||||||
@@ -32,13 +31,31 @@ public static class Logger_Samples
|
|||||||
|
|
||||||
Console.WriteLine("MQTT client is connected.");
|
Console.WriteLine("MQTT client is connected.");
|
||||||
|
|
||||||
var mqttClientDisconnectOptions = mqttFactory.CreateClientDisconnectOptionsBuilder()
|
var mqttClientDisconnectOptions = mqttFactory.CreateClientDisconnectOptionsBuilder().Build();
|
||||||
.Build();
|
|
||||||
|
|
||||||
await mqttClient.DisconnectAsync(mqttClientDisconnectOptions, CancellationToken.None);
|
await mqttClient.DisconnectAsync(mqttClientDisconnectOptions, CancellationToken.None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async Task Log_To_Console()
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* This sample covers the creation of a custom logger which is used to log to the console.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var mqttFactory = new MqttFactory(new ConsoleLogger());
|
||||||
|
|
||||||
|
var mqttClientOptions = mqttFactory.CreateClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build();
|
||||||
|
|
||||||
|
using (var mqttClient = mqttFactory.CreateMqttClient())
|
||||||
|
{
|
||||||
|
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
|
||||||
|
|
||||||
|
Console.WriteLine("MQTT client is connected. Press any key to exit.");
|
||||||
|
Console.ReadKey(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static async Task Use_Event_Logger()
|
public static async Task Use_Event_Logger()
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
@@ -64,9 +81,7 @@ public static class Logger_Samples
|
|||||||
|
|
||||||
var mqttFactory = new MqttFactory(mqttEventLogger);
|
var mqttFactory = new MqttFactory(mqttEventLogger);
|
||||||
|
|
||||||
var mqttClientOptions = mqttFactory.CreateClientOptionsBuilder()
|
var mqttClientOptions = mqttFactory.CreateClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build();
|
||||||
.WithTcpServer("broker.hivemq.com")
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
using (var mqttClient = mqttFactory.CreateMqttClient())
|
using (var mqttClient = mqttFactory.CreateMqttClient())
|
||||||
{
|
{
|
||||||
@@ -74,8 +89,7 @@ public static class Logger_Samples
|
|||||||
|
|
||||||
Console.WriteLine("MQTT client is connected.");
|
Console.WriteLine("MQTT client is connected.");
|
||||||
|
|
||||||
var mqttClientDisconnectOptions = mqttFactory.CreateClientDisconnectOptionsBuilder()
|
var mqttClientDisconnectOptions = mqttFactory.CreateClientDisconnectOptionsBuilder().Build();
|
||||||
.Build();
|
|
||||||
|
|
||||||
await mqttClient.DisconnectAsync(mqttClientDisconnectOptions, CancellationToken.None);
|
await mqttClient.DisconnectAsync(mqttClientDisconnectOptions, CancellationToken.None);
|
||||||
}
|
}
|
||||||
@@ -83,7 +97,7 @@ public static class Logger_Samples
|
|||||||
|
|
||||||
sealed class MyLogger : IMqttNetLogger
|
sealed class MyLogger : IMqttNetLogger
|
||||||
{
|
{
|
||||||
public bool IsEnabled { get; set; } = true;
|
public bool IsEnabled { get; } = true;
|
||||||
|
|
||||||
public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception)
|
public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ Console.WriteLine("Welcome to MQTTnet samples!");
|
|||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
|
|
||||||
var sampleClasses = Assembly.GetExecutingAssembly().GetExportedTypes().OrderBy(c => c.Name).ToList();
|
var sampleClasses = Assembly.GetExecutingAssembly().GetExportedTypes().OrderBy(c => c.Name).ToList();
|
||||||
|
sampleClasses.RemoveAll(c => c.Namespace.Contains(".Shared"));
|
||||||
|
|
||||||
var index = 0;
|
var index = 0;
|
||||||
foreach (var sampleClass in sampleClasses)
|
foreach (var sampleClass in sampleClasses)
|
||||||
|
|||||||
@@ -6,8 +6,8 @@
|
|||||||
// ReSharper disable UnusedMember.Global
|
// ReSharper disable UnusedMember.Global
|
||||||
// ReSharper disable InconsistentNaming
|
// ReSharper disable InconsistentNaming
|
||||||
|
|
||||||
using MQTTnet.Diagnostics;
|
|
||||||
using MQTTnet.Protocol;
|
using MQTTnet.Protocol;
|
||||||
|
using MQTTnet.Samples.Shared;
|
||||||
using MQTTnet.Server;
|
using MQTTnet.Server;
|
||||||
|
|
||||||
namespace MQTTnet.Samples.Server;
|
namespace MQTTnet.Samples.Server;
|
||||||
@@ -170,50 +170,4 @@ public static class Server_Simple_Samples
|
|||||||
await server.StartAsync();
|
await server.StartAsync();
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
class ConsoleLogger : IMqttNetLogger
|
|
||||||
{
|
|
||||||
readonly object _consoleSyncRoot = new();
|
|
||||||
|
|
||||||
public bool IsEnabled => true;
|
|
||||||
|
|
||||||
public void Publish(MqttNetLogLevel logLevel, string source, string message, object[]? parameters, Exception? exception)
|
|
||||||
{
|
|
||||||
var foregroundColor = ConsoleColor.White;
|
|
||||||
switch (logLevel)
|
|
||||||
{
|
|
||||||
case MqttNetLogLevel.Verbose:
|
|
||||||
foregroundColor = ConsoleColor.White;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MqttNetLogLevel.Info:
|
|
||||||
foregroundColor = ConsoleColor.Green;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MqttNetLogLevel.Warning:
|
|
||||||
foregroundColor = ConsoleColor.DarkYellow;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MqttNetLogLevel.Error:
|
|
||||||
foregroundColor = ConsoleColor.Red;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parameters?.Length > 0)
|
|
||||||
{
|
|
||||||
message = string.Format(message, parameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
lock (_consoleSyncRoot)
|
|
||||||
{
|
|
||||||
Console.ForegroundColor = foregroundColor;
|
|
||||||
Console.WriteLine(message);
|
|
||||||
|
|
||||||
if (exception != null)
|
|
||||||
{
|
|
||||||
Console.WriteLine(exception);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
// 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 MQTTnet.Diagnostics;
|
||||||
|
|
||||||
|
namespace MQTTnet.Samples.Shared;
|
||||||
|
|
||||||
|
public sealed class ConsoleLogger : IMqttNetLogger
|
||||||
|
{
|
||||||
|
readonly object _consoleSyncRoot = new();
|
||||||
|
|
||||||
|
public bool IsEnabled => true;
|
||||||
|
|
||||||
|
public void Publish(MqttNetLogLevel logLevel, string source, string message, object[]? parameters, Exception? exception)
|
||||||
|
{
|
||||||
|
var foregroundColor = ConsoleColor.White;
|
||||||
|
switch (logLevel)
|
||||||
|
{
|
||||||
|
case MqttNetLogLevel.Verbose:
|
||||||
|
foregroundColor = ConsoleColor.White;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MqttNetLogLevel.Info:
|
||||||
|
foregroundColor = ConsoleColor.Green;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MqttNetLogLevel.Warning:
|
||||||
|
foregroundColor = ConsoleColor.DarkYellow;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MqttNetLogLevel.Error:
|
||||||
|
foregroundColor = ConsoleColor.Red;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parameters?.Length > 0)
|
||||||
|
{
|
||||||
|
message = string.Format(message, parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
lock (_consoleSyncRoot)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = foregroundColor;
|
||||||
|
Console.WriteLine(message);
|
||||||
|
|
||||||
|
if (exception != null)
|
||||||
|
{
|
||||||
|
Console.WriteLine(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace MQTTnet.Samples.Helpers;
|
namespace MQTTnet.Samples.Shared;
|
||||||
|
|
||||||
internal static class ObjectExtensions
|
internal static class ObjectExtensions
|
||||||
{
|
{
|
||||||
Reference in New Issue
Block a user