问题
I'm working on a MVC application that serves as a MQTT client. I'm using the C# library M2Mqtt from paho. Basicly every time someone presses a button on my website, I want a message to be published, and it will recieve an answer aswell (thus it subscribes to a topic aswell.)
My question is: do I have to connect to the broker every time again when pressing the button (calling an ActionResult that publishes the message) or is there a way to keep the connection alive (f.e. initializing it in Global?) and how would one do that then? As I'm fairly new to MVC/.net programming I'm not familiar with all ins and outs, and am kind of struggling to figure this out on my own. Any help on the matter is much appreciated! :-)
回答1:
I'm the M2Mqtt developer :-) When you use Connect method in the M2Mqtt client, the connection is still alive thanks to a keep alive mechanism with ping messages. If you don't send message for the specified timeout, the client mantains it alive using ping messages with the broker.
You don't need to connect for every message to send .. you can connect one time.
Paolo
回答2:
If you don not want to reconnect again and again then there should be a mechanism that will ping with any notify message continuously to keep it alive, otherwise it will time out, and if time out then it required to reconnect
来源:https://stackoverflow.com/questions/29228445/mqtt-client-in-mvc-application