nmqtt icon indicating copy to clipboard operation
nmqtt copied to clipboard

Make MqttClient re-usable so you can connect and disconnect at will

Open markallanson opened this issue 11 years ago • 2 comments

Currently an MqttClient is single use - meaning you need to dispose and create a new one to reconnect. It would be useful to be able to pull the connection up and down and have subscriptions re-sync without having to do these cleanups.

markallanson avatar Oct 20 '13 20:10 markallanson

How can I catch the fact that I have to re-connect. I'm currently wrapping nmqtt in a try-catch block but I cannot catch the fact that nmqtt is disconnected in order to do a dispose and reconnect (I've tested by shutting down the MQTT-broker)

{UnhandledExceptionController_Framework} --<Critical> -- 2013-11-27 08:55:30 -- Exception: System.ObjectDisposedException Level 1) Cannot access a disposed object. Object name: 'System.Net.Sockets.NetworkStream'. StackTrace for Level 1: at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size) at Nmqtt.MqttConnection.Send(Byte[] message) at Nmqtt.MqttConnection.Send(Stream message) at Nmqtt.MqttConnectionHandler.SendMessage(MqttMessage message) at Nmqtt.MqttConnectionKeepAlive.PingRequired(Object state) at System.Threading._TimerCallback.TimerCallback_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading._TimerCallback.PerformTimerCallback(Object state)

Appreciate the help :-)

nidayand avatar Nov 27 '13 08:11 nidayand

Unfortunately there is no nice way of doing this at the moment.

The connection state is exposed on the MqttClient class and you can monitor that, but there is no pro-active notification. It’s a big hole I need to fill!

On Wednesday, 27 November 2013 at 08:26, Peter wrote:

How can I catch the fact that I have to re-connect. I'm currently wrapping nmqtt in a try-catch block but I cannot catch the fact that nmqtt is disconnected in order to do a dispose and reconnect (I've tested by shutting down the MQTT-broker) {UnhandledExceptionController_Framework} -- -- 2013-11-27 08:55:30 -- Exception:
System.ObjectDisposedException Level 1) Cannot access a disposed object. Object name: 'System.Net.Sockets.NetworkStream'. StackTrace for Level 1: at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size) at Nmqtt.MqttConnection.Send(Byte[] message) at Nmqtt.MqttConnection.Send(Stream message) at Nmqtt.MqttConnectionHandler.SendMessage(MqttMessage message) at Nmqtt.MqttConnectionKeepAlive.PingRequired(Object state) at System.Threading.TimerCallback.TimerCallbackContext(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading._TimerCallback.PerformTimerCallback(Object state)
Appreciate the help :-)

— Reply to this email directly or view it on GitHub (https://github.com/markallanson/nmqtt/issues/7#issuecomment-29367226).

markallanson avatar Nov 28 '13 15:11 markallanson