mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-17 17:53:03 +08:00
Update documentation
This commit is contained in:
@@ -12,15 +12,21 @@ namespace MQTTnet.Core.Client
|
||||
|
||||
private MqttClientTlsOptions _tlsOptions;
|
||||
|
||||
public MqttClientOptionsBuilder WithProtocolVersion(MqttProtocolVersion protocolVersion)
|
||||
public MqttClientOptionsBuilder WithLogId(string value)
|
||||
{
|
||||
_options.ProtocolVersion = protocolVersion;
|
||||
_options.LogId = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MqttClientOptionsBuilder WithCommunicationTimeout(TimeSpan communicationTimeout)
|
||||
public MqttClientOptionsBuilder WithProtocolVersion(MqttProtocolVersion value)
|
||||
{
|
||||
_options.CommunicationTimeout = communicationTimeout;
|
||||
_options.ProtocolVersion = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MqttClientOptionsBuilder WithCommunicationTimeout(TimeSpan value)
|
||||
{
|
||||
_options.CommunicationTimeout = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -30,21 +36,21 @@ namespace MQTTnet.Core.Client
|
||||
return this;
|
||||
}
|
||||
|
||||
public MqttClientOptionsBuilder WithKeepAlivePeriod(TimeSpan keepAlivePeriod)
|
||||
public MqttClientOptionsBuilder WithKeepAlivePeriod(TimeSpan value)
|
||||
{
|
||||
_options.KeepAlivePeriod = keepAlivePeriod;
|
||||
_options.KeepAlivePeriod = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MqttClientOptionsBuilder WithClientId(string clientId)
|
||||
public MqttClientOptionsBuilder WithClientId(string value)
|
||||
{
|
||||
_options.ClientId = clientId;
|
||||
_options.ClientId = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MqttClientOptionsBuilder WithWillMessage(MqttApplicationMessage applicationMessage)
|
||||
public MqttClientOptionsBuilder WithWillMessage(MqttApplicationMessage value)
|
||||
{
|
||||
_options.WillMessage = applicationMessage;
|
||||
_options.WillMessage = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -137,6 +137,12 @@
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging">
|
||||
<Version>1.1.2</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console">
|
||||
<Version>1.1.2</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
|
||||
<Version>5.4.0</Version>
|
||||
</PackageReference>
|
||||
|
||||
@@ -5,6 +5,7 @@ using Windows.Security.Cryptography.Certificates;
|
||||
using Windows.UI.Core;
|
||||
using Windows.UI.Xaml;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MQTTnet.Core;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Diagnostics;
|
||||
@@ -111,7 +112,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
{
|
||||
ReceivedMessages.Items.Add(item);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
private async void Publish(object sender, RoutedEventArgs e)
|
||||
@@ -227,16 +228,50 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
private async Task WikiCode()
|
||||
{
|
||||
{
|
||||
// Create a new MQTT client
|
||||
// Write all trace messages to the console window.
|
||||
MqttNetTrace.TraceMessagePublished += (s, e) =>
|
||||
{
|
||||
Console.WriteLine($">> [{e.TraceMessage.Timestamp:O}] [{e.TraceMessage.ThreadId}] [{e.TraceMessage.Source}] [{e.TraceMessage.Level}]: {e.TraceMessage.Message}");
|
||||
if (e.TraceMessage.Exception != null)
|
||||
{
|
||||
Console.WriteLine(e.TraceMessage.Exception);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
{
|
||||
// Add the console logger to the logger factory.
|
||||
var services = new ServiceCollection()
|
||||
.AddMqttClient()
|
||||
.BuildServiceProvider();
|
||||
|
||||
var factory = new MqttFactory(services);
|
||||
var client = factory.CreateMqttClient();
|
||||
services.GetRequiredService<ILoggerFactory>()
|
||||
.AddConsole();
|
||||
}
|
||||
|
||||
{
|
||||
// Use a custom identifier for the trace messages.
|
||||
var clientOptions = new MqttClientOptionsBuilder()
|
||||
.WithLogId("ClientX")
|
||||
.Build();
|
||||
}
|
||||
|
||||
{
|
||||
// Create a client from the service provider manually.
|
||||
var serviceProvider = new ServiceCollection()
|
||||
.AddMqttClient()
|
||||
.BuildServiceProvider();
|
||||
|
||||
var mqttClient = serviceProvider.GetRequiredService<IMqttClient>();
|
||||
}
|
||||
|
||||
{
|
||||
// Create a new MQTT client.
|
||||
var factory = new MqttFactory();
|
||||
var mqttClient = factory.CreateMqttClient();
|
||||
|
||||
{
|
||||
// Create TCP based options using the builder
|
||||
// Create TCP based options using the builder.
|
||||
var options = new MqttClientOptionsBuilder()
|
||||
.WithClientId("Client1")
|
||||
.WithTcpServer("broker.hivemq.com")
|
||||
@@ -245,7 +280,31 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
.WithCleanSession()
|
||||
.Build();
|
||||
|
||||
await client.ConnectAsync(options);
|
||||
await mqttClient.ConnectAsync(options);
|
||||
}
|
||||
|
||||
{
|
||||
// Use TCP connection.
|
||||
var options = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("broker.hivemq.com", 1883) // Port is optional
|
||||
.Build();
|
||||
}
|
||||
|
||||
{
|
||||
// Use secure TCP connection.
|
||||
var options = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("broker.hivemq.com")
|
||||
.WithTls()
|
||||
.Build();
|
||||
}
|
||||
|
||||
{
|
||||
// Use WebSocket connection.
|
||||
var options = new MqttClientOptionsBuilder()
|
||||
.WithWebSocketServer("broker.hivemq.com:8000/mqtt")
|
||||
.Build();
|
||||
|
||||
await mqttClient.ConnectAsync(options);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -271,10 +330,10 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
|
||||
{
|
||||
// Subscribe to a topic
|
||||
await client.SubscribeAsync(new TopicFilterBuilder().WithTopic("my/topic").Build());
|
||||
await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic("my/topic").Build());
|
||||
|
||||
// Unsubscribe from a topic
|
||||
await client.UnsubscribeAsync("my/topic");
|
||||
await mqttClient.UnsubscribeAsync("my/topic");
|
||||
|
||||
// Publish an application message
|
||||
var applicationMessage = new MqttApplicationMessageBuilder()
|
||||
@@ -283,7 +342,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
.WithAtLeastOnceQoS()
|
||||
.Build();
|
||||
|
||||
await client.PublishAsync(applicationMessage);
|
||||
await mqttClient.PublishAsync(applicationMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user