I'm trying to use M2MQTT library to send MQTT messages to an Azure IoT Hub. I've followed the guidance here IoT Hub MQTT support to come up with the right parameters for connect and so on in the native MQTT section of that article.
I'm connecting successfully (CONNACK return value 0) with the following code:
_publishClient = new MqttClient(_hostName, 8883, true, null, null,MqttSslProtocols.TLSv1_2);
_publishClient.MqttMsgPublished += _client_MqttMsgPublished;
_publishClient.ConnectionClosed += _client_ConnectionClosed;
var connack = _publishClient.Connect(_publishDeviceId, string.Format("{0}/{1}",
_hostName, _publishDeviceId), _publishSas,true,3600);
but as soon as I make a Publish call (where deviceid is my device id):
var pubresult = _publishClient.Publish("/devices/{deviceid}/messages/events/", Encoding.UTF8.GetBytes("Hello"), MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false);
M2MQTT disconnects and raises the OnConnectionClosed event, and the message never gets delivered to the Azure IoT Hub.
I've tried stepping down through the M2MQTT MqttClient class code and the disconnect is coming from the ReceiveThread with the comments making it sound like it is Azure IoT Hub end that closed the connection:
// zero bytes read, peer gracefully closed socket
else
{
// wake up thread that will notify connection is closing
this.OnConnectionClosing();
}
If anyone has any ideas on why it is closing or how to troubleshoot, I'm all ears/eyes.