MQTT client in MVC application

早过忘川 提交于 2019-12-11 08:24:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!