-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
37 lines (32 loc) · 1.18 KB
/
Copy pathProgram.cs
File metadata and controls
37 lines (32 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using MQTTnet.Server;
using MQTTnet;
using System.Text;
using static System.Console;
// Create the options for MQTT Broker
var options = new MqttServerOptionsBuilder()
//Set endpoint to localhost
.WithDefaultEndpoint()
// Port going to use 5004
.WithDefaultEndpointPort(5004);
// Create a new mqtt server
var server = new MqttFactory().CreateMqttServer(options.Build());
//Add Interceptor for logging incoming messages
server.InterceptingPublishAsync += Server_InterceptingPublishAsync;
// Start the server
await server.StartAsync();
// Keep application running until user press a key
ReadLine();
Task Server_InterceptingPublishAsync(InterceptingPublishEventArgs arg)
{
// Convert Payload to string
var payload = arg.ApplicationMessage?.Payload == null ? null : Encoding.UTF8.GetString(arg.ApplicationMessage?.Payload);
WriteLine(
" TimeStamp: {0} -- Message: ClientId = {1}, Topic = {2}, Payload = {3}, QoS = {4}, Retain-Flag = {5}",
DateTime.Now,
arg.ClientId,
arg.ApplicationMessage?.Topic,
payload,
arg.ApplicationMessage?.QualityOfServiceLevel,
arg.ApplicationMessage?.Retain);
return Task.CompletedTask;
}