1

In my current .NET-project I want to publish a message over MQTT to a broker, but I tried several ways to implement this, but none of them works. I would like to use M2Mqtt (Paho Link), but I also tried MqttDotNet with no success.

My current way to solve this problem is to get the easiest possible implementation to work, but even this one does not work. I'm just creating an instance of MqttClient, connect to the broker (debugging shows me this works via 'IsConnected') and then I publish a message (simple string 'Local:0:user:dev'). Nevertheless my broker never receives this message and the only reaction I'm getting from VisualStudio are these 4 lines:

Der Thread 0x13a8 hat mit Code 259 (0x103) geendet. Der Thread 0x16d4 hat mit Code 259 (0x103) geendet. Der Thread 0x1ab0 hat mit Code 259 (0x103) geendet. Der Thread 0xa48 hat mit Code 259 (0x103) geendet.

This message is german and it is identical with the thread exiting message mentioned here.

I'm using this code to connect, publish and disconnect and I already tried the same code without the 'disconnect'-part.

MqttClient client = new MqttClient("127.0.0.1");
client.Connect("client");
client.Publish("cdj/new", Encoding.UTF8.GetBytes(message));
client.Disconnect();

Just to make sure, the broker is working and it can receive exactly the same message sent via Paho MQTT Utility. I'm using .NET 4.0 and also M2Mqtt version 4.0 (because the newest version is for .NET 4.5).

I don't know what I should do different, especially because there is no exeption or error message indicating what is going wrong there.

2
  • I'm not familiar with the .net code, but is there a onConnected callback? if so move the Publish and Disconnect calls to inside the onConnected callback. You may be trying to send before the connection has compleated Commented Sep 26, 2015 at 10:25
  • There is no onConnected callback and the missing connection was not the problem, but it seems like I (via 'Disconnect') or the GC was closing the socket during the publish operation. Now I'm holding the connection as long as possible to send multiple messages, which should be better practice anyhow ;) Commented Sep 26, 2015 at 22:23

1 Answer 1

1

It seems like either I via code or the GC was closing the network socket during the publish operation, so now my solution is to hold the connection to the broker as long as possible to send multiple messages and its working.

As a solution to close the socket right after the publish operation is finished, I would suggest to use the MqttMsgPublished event to disconnect the client.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.