Skip to content

Commit e7ce2b6

Browse files
author
Anton Vishnyak
committed
Added example client
1 parent 475413b commit e7ce2b6

12 files changed

+72
-6
lines changed

ExampleClient/ExampleClient.csproj

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="3.0.12" />
10+
</ItemGroup>
11+
12+
</Project>

ExampleClient/Program.cs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using MQTTnet;
2+
using MQTTnet.Client.Options;
3+
using MQTTnet.Extensions.ManagedClient;
4+
using System;
5+
6+
namespace ExampleClient
7+
{
8+
internal class Program
9+
{
10+
private static async System.Threading.Tasks.Task Main(string[] args)
11+
{
12+
// Setup and start a managed MQTT client.
13+
var options = new ManagedMqttClientOptionsBuilder()
14+
.WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
15+
.WithClientOptions(new MqttClientOptionsBuilder()
16+
.WithClientId("Client1")
17+
.WithWebSocketServer("localhost:50482/mqtt")
18+
.Build())
19+
.Build();
20+
21+
var mqttClient = new MqttFactory().CreateManagedMqttClient();
22+
await mqttClient.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic("MqttWeatherForecast/90210/temperature").Build());
23+
24+
mqttClient.UseConnectedHandler(e =>
25+
{
26+
Console.WriteLine($"Connection Result: {e.AuthenticateResult.ResultCode}");
27+
});
28+
29+
mqttClient.UseApplicationMessageReceivedHandler(e =>
30+
{
31+
Console.WriteLine($"Message from {e.ClientId}: {e.ApplicationMessage.Payload.Length} bytes.");
32+
});
33+
34+
await mqttClient.StartAsync(options);
35+
36+
await mqttClient.PublishAsync(new ManagedMqttApplicationMessageBuilder().WithApplicationMessage(msg =>
37+
{
38+
msg.WithAtLeastOnceQoS();
39+
msg.WithPayload(BitConverter.GetBytes(98.6d));
40+
msg.WithTopic("MqttWeatherForecast/90210/temperature");
41+
}).Build());
42+
43+
// StartAsync returns immediately, as it starts a new thread using Task.Run, and so the calling thread needs
44+
// to wait.
45+
Console.ReadLine();
46+
}
47+
}
48+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

MQTTnet.AspNetCore.AttributeRouting.sln

+12-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ VisualStudioVersion = 16.0.30406.217
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.AspNetCore.AttributeRouting", "Source\MQTTnet.AspNetCore.AttributeRouting.csproj", "{B1CFA8BF-FEBA-4978-86A6-D052C4C44BA8}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example", "Example\Example.csproj", "{EAF719B1-BDB9-433C-BAC6-D07BE56A9796}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.AspNetCore.AttributeRouting.Tests", "Tests\MQTTnetA.AspNetCore.AttributeRouting.Tests\MQTTnet.AspNetCore.AttributeRouting.Tests.csproj", "{BF9B933B-6EE0-4CEA-ACAF-DF425DF0B862}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.AspNetCore.AttributeRouting.Tests", "Tests\MQTTnetA.AspNetCore.AttributeRouting.Tests\MQTTnet.AspNetCore.AttributeRouting.Tests.csproj", "{BF9B933B-6EE0-4CEA-ACAF-DF425DF0B862}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleServer", "ExampleServer\ExampleServer.csproj", "{3B9BD1C8-FDEC-4BFF-A8DC-E1F6B6C539A9}"
11+
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleClient", "ExampleClient\ExampleClient.csproj", "{C832D7F0-C0DA-403A-84DD-1682D44A3A3A}"
1113
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -19,14 +21,18 @@ Global
1921
{B1CFA8BF-FEBA-4978-86A6-D052C4C44BA8}.Debug|Any CPU.Build.0 = Debug|Any CPU
2022
{B1CFA8BF-FEBA-4978-86A6-D052C4C44BA8}.Release|Any CPU.ActiveCfg = Release|Any CPU
2123
{B1CFA8BF-FEBA-4978-86A6-D052C4C44BA8}.Release|Any CPU.Build.0 = Release|Any CPU
22-
{EAF719B1-BDB9-433C-BAC6-D07BE56A9796}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23-
{EAF719B1-BDB9-433C-BAC6-D07BE56A9796}.Debug|Any CPU.Build.0 = Debug|Any CPU
24-
{EAF719B1-BDB9-433C-BAC6-D07BE56A9796}.Release|Any CPU.ActiveCfg = Release|Any CPU
25-
{EAF719B1-BDB9-433C-BAC6-D07BE56A9796}.Release|Any CPU.Build.0 = Release|Any CPU
2624
{BF9B933B-6EE0-4CEA-ACAF-DF425DF0B862}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2725
{BF9B933B-6EE0-4CEA-ACAF-DF425DF0B862}.Debug|Any CPU.Build.0 = Debug|Any CPU
2826
{BF9B933B-6EE0-4CEA-ACAF-DF425DF0B862}.Release|Any CPU.ActiveCfg = Release|Any CPU
2927
{BF9B933B-6EE0-4CEA-ACAF-DF425DF0B862}.Release|Any CPU.Build.0 = Release|Any CPU
28+
{3B9BD1C8-FDEC-4BFF-A8DC-E1F6B6C539A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{3B9BD1C8-FDEC-4BFF-A8DC-E1F6B6C539A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{3B9BD1C8-FDEC-4BFF-A8DC-E1F6B6C539A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{3B9BD1C8-FDEC-4BFF-A8DC-E1F6B6C539A9}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{C832D7F0-C0DA-403A-84DD-1682D44A3A3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{C832D7F0-C0DA-403A-84DD-1682D44A3A3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{C832D7F0-C0DA-403A-84DD-1682D44A3A3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{C832D7F0-C0DA-403A-84DD-1682D44A3A3A}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)