Skip to content

Commit

Permalink
Merge pull request #9 from JonasMH/develop
Browse files Browse the repository at this point in the history
Add OnConnect/Disconnect to MqttConService
  • Loading branch information
JonasMH authored Jun 9, 2022
2 parents 561d7ef + 6f98a3f commit d635f31
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/ToMqttNet/MqttConnectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class MqttConnectionService : BackgroundService, IMqttConnectionService
private IManagedMqttClient? _mqttClient;

public event EventHandler<MqttApplicationMessageReceivedEventArgs> OnApplicationMessageReceived = null!;
public event EventHandler<EventArgs>? OnConnect;
public event EventHandler<EventArgs>? OnDisconnect;

public MqttConnectionService(
ILogger<MqttConnectionService> logger,
Expand Down Expand Up @@ -57,11 +59,14 @@ await _mqttClient.PublishAsync(
.WithTopic($"{MqttOptions.NodeId}/connected")
.WithRetainFlag()
.Build());
OnConnect?.Invoke(this, new EventArgs());
});

_mqttClient.UseDisconnectedHandler((evnt) =>
{
_logger.LogInformation(evnt.Exception, "Disconnected from mqtt: {reason}", evnt.Reason);
OnDisconnect?.Invoke(this, new EventArgs());
});

_mqttClient.UseApplicationMessageReceivedHandler((evnt) =>
Expand Down

0 comments on commit d635f31

Please sign in to comment.