Skip to content

Commit

Permalink
Example: SubscriberWithEvents (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
pglombardo authored Apr 16, 2024
1 parent eeac24c commit ab49e83
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 13 deletions.
60 changes: 47 additions & 13 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down
76 changes: 76 additions & 0 deletions Examples/SubscriberWithEvents/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
3 changes: 3 additions & 0 deletions Examples/SubscriberWithEvents/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SubscriberWithEvents

Example app that I used to measure subscription event processing.
14 changes: 14 additions & 0 deletions Examples/SubscriberWithEvents/SubscriberWithEvents.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Source\HiveMQtt\HiveMQtt.csproj" />
</ItemGroup>

</Project>

0 comments on commit ab49e83

Please sign in to comment.