diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 81c1eb4d..299d19b1 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -2,40 +2,49 @@ "version": "2.0.0", "tasks": [ { - "label": "build", + "label": "build HiveMQtt", "command": "dotnet", "type": "process", "args": [ "build", - "${workspaceFolder}/Tests/HiveMQtt.Test/HiveMQtt.Test.csproj", + "${workspaceFolder}/Source/HiveMQtt/HiveMQtt.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], - "problemMatcher": "$msCompile" + "problemMatcher": "$msCompile", + "group": { + "kind": "build", + "isDefault": true + } }, { - "label": "publish", + "label": "clean HiveMQtt", "command": "dotnet", "type": "process", "args": [ - "publish", - "${workspaceFolder}/Tests/HiveMQtt.Test/HiveMQtt.Test.csproj", + "clean", + "${workspaceFolder}/Source/HiveMQtt/HiveMQtt.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], - "problemMatcher": "$msCompile" + "problemMatcher": "$msCompile", + "group": { + "kind": "build", + "isDefault": true + } }, { - "label": "watch", + "label": "build HiveMQtt.Test", "command": "dotnet", "type": "process", "args": [ - "watch", - "run", - "--project", - "${workspaceFolder}/Tests/HiveMQtt.Test/HiveMQtt.Test.csproj" + "build", + "${workspaceFolder}/Tests/HiveMQtt.Test/HiveMQtt.Test.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" ], - "problemMatcher": "$msCompile" + "problemMatcher": "$msCompile", + "group": "build" }, { "label": "build ClientBenchmarkApp", @@ -46,6 +55,31 @@ "${workspaceFolder}/Benchmarks/ClientBenchmarkApp/ClientBenchmarkApp.csproj" ], "problemMatcher": "$msCompile", + "group": "build" + }, + { + "label": "build SubscriberWithEvents", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/Examples/SubscriberWithEvents/SubscriberWithEvents.csproj" + ], + "problemMatcher": "$msCompile", + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "label": "clean SubscriberWithEvents", + "command": "dotnet", + "type": "process", + "args": [ + "clean", + "${workspaceFolder}/Examples/SubscriberWithEvents/SubscriberWithEvents.csproj" + ], + "problemMatcher": "$msCompile", "group": { "kind": "build", "isDefault": true diff --git a/Examples/SubscriberWithEvents/Program.cs b/Examples/SubscriberWithEvents/Program.cs new file mode 100644 index 00000000..38c64855 --- /dev/null +++ b/Examples/SubscriberWithEvents/Program.cs @@ -0,0 +1,76 @@ +namespace SubscriberWithEvents; + +using System.Text; +using System.Text.Json; +using HiveMQtt.Client; +using HiveMQtt.Client.Options; +using HiveMQtt.MQTT5.Types; + +public class Program +{ + public static bool ExitRequested { get; set; } + public static int MessageCount { get; set; } + public static int PublishesReceivedCount { get; set; } + + public static async Task Main(string[] args) + { + MessageCount = 0; + PublishesReceivedCount = 0; + + // Subscribe to the CancelKeyPress event + Console.CancelKeyPress += (sender, e) => + { + // Handle Ctrl+C (SIGINT) by setting exitRequested flag + e.Cancel = true; // Prevent process termination + ExitRequested = true; + Console.WriteLine("Ctrl+C (SIGINT) received. Press Ctrl+C again to exit immediately."); + }; + + var options = new HiveMQClientOptions + { + Host = "127.0.0.1", + Port = 1883, + CleanStart = true, + ClientId = "SubscriberWithEvents", + }; + + var client = new HiveMQClient(options); + + // Message Handler + // + // It's important that this is setup before we connect to the broker + // otherwise queued messages that are sent down may be lost. + // + client.OnMessageReceived += (sender, args) => + { + MessageCount++; + }; + + // client.OnPublishReceived += (sender, args) => + // { + // PublishesReceivedCount++; + // }; + + // Connect to the broker + var connectResult = await client.ConnectAsync().ConfigureAwait(false); + if (connectResult.ReasonCode != HiveMQtt.MQTT5.ReasonCodes.ConnAckReasonCode.Success) + { + throw new IOException($"Failed to connect: {connectResult.ReasonString}"); + } + + // Subscribe to a topic + var topic = "load/test/1"; + var subscribeResult = await client.SubscribeAsync(topic, QualityOfService.ExactlyOnceDelivery).ConfigureAwait(false); + Console.WriteLine($"Subscribed to {topic}: {subscribeResult.Subscriptions[0].SubscribeReasonCode}"); + + var message_number = 0; + while (!ExitRequested) + { + await Task.Delay(1000).ConfigureAwait(false); + Console.WriteLine($"Received {MessageCount} msgs/sec"); + // Console.WriteLine($"Received {MessageCount} msgs/sec & {PublishesReceivedCount} publishes/sec"); + MessageCount = 0; + PublishesReceivedCount = 0; + } + } +} diff --git a/Examples/SubscriberWithEvents/README.md b/Examples/SubscriberWithEvents/README.md new file mode 100644 index 00000000..dd6a997e --- /dev/null +++ b/Examples/SubscriberWithEvents/README.md @@ -0,0 +1,3 @@ +# SubscriberWithEvents + +Example app that I used to measure subscription event processing. diff --git a/Examples/SubscriberWithEvents/SubscriberWithEvents.csproj b/Examples/SubscriberWithEvents/SubscriberWithEvents.csproj new file mode 100644 index 00000000..3ab1e3eb --- /dev/null +++ b/Examples/SubscriberWithEvents/SubscriberWithEvents.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + +