简体   繁体   English

订阅者未主动接收已发布的消息

[英]subscriber not actively receiving published message

I have successfully connected MQTT to my broker both publisher and subscriber are working fine but there is a problem my subscriber is not actively receiving the message, the function mqtt_publishedRecieved triggers only one time,when I restart the subscriber app and only one message receives at a time.我已经成功地将 MQTT 连接到我的经纪人,发布者和订阅者都工作正常,但有一个问题,我的订阅者没有主动接收消息,函数 mqtt_publishedRecieved 仅触发一次,当我重新启动订阅者应用程序并且一次只收到一条消息时时间。 in order to get another message, I have to restart my app again.为了收到另一条消息,我必须再次重新启动我的应用程序。 Well, as per my understanding it is because I called the config method of my class at startup.嗯,根据我的理解,这是因为我在启动时调用了我的类的 config 方法。 so therefore It checks for the subscribed topic only at startup.因此,它仅在启动时检查订阅的主题。 But I really want that functionality in my subscriber class to immediately receive the message once it is published by the receiver, which means both should work parallel.但是我真的希望我的订阅者类中的功能在接收者发布消息后立即接收消息,这意味着两者应该并行工作。 My requirement is => if subscriber is connected, receives the message as soon as publisher fires.我的要求是 => 如果订阅者已连接,则在发布者触发后立即接收消息。 => if the subscriber is disconnected, message should be queued and later on all message will receive when subscriber connected again. => 如果订阅者断开连接,则消息应排队,稍后当订阅者再次连接时,所有消息都将收到。

I do a research mqtt clean session set to false will ensure the persistent session so I set that flag to false to occupy persistent session, but it won't work for me.我做了一个研究 mqtt clean session 设置为 false 将确保持久会话,所以我将该标志设置为 false 以占用持久会话,但它对我不起作用。

it seems to me that I should add functionality to receive a message, for example, a button, so when the button is clicked it starts to get a message, but I can't set trigger/callback/button to receive the message for the subscribed topic.在我看来,我应该添加接收消息的功能,例如按钮,所以当按钮被点击时,它开始收到一条消息,但我无法设置触发器/回调/按钮来接收消息订阅的主题。 My app should start receiving all the published messages when service is started and stopped when it is disconnected.我的应用程序应该在服务启动时开始接收所有发布的消息,并在断开连接时停止。

below is the code of the publisher class.下面是发布者类的代码。

public class publisher : IDatabaseSubscription
    {
public publisher()
        {

                    _mqttClient = new MqttClient("127.0.0.1");
                    _mqttClient.Connect("clientId", null, null, false, 60);
                
              
            }
 private void MQTT_OnChanged(object sender, RecordChangedEventArgs<EventDto> e)
        {
           
                                    if (_mqttClient != null && _mqttClient.IsConnected)
                                    {
                                      var message = System.Text.Encoding.UTF8.GetBytes("Hello from app 1");
                                        var statusCode = _mqttClient.Publish("Message1",message , MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
                                        
                                }
                            }
    }

below is the code of subscriber class下面是订阅者类的代码

 public class subscriber
    {
        private MqttClient _mqttClient;
        public subscriber()
        {
           
           
        }
        public void configure ()
        {
            _mqttClient = new MqttClient("127.0.0.1");
            _mqttClient.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
            var status = _mqttClient.Subscribe(new string[] { "Message1" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
            _mqttClient.Connect("clientId", null, null , false, 60) ;
        }

        private void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            var message = System.Text.Encoding.UTF8.GetString(e.Message);
        }
}

below is the code in startup下面是启动时的代码

if(IsSubscriptionEnabled())
            {
                var service = _container.GetInstance<subscription>();
                service.configure();
            }
        
    

这是由于发布者和订阅者中的客户端 ID 相同,如果新客户端连接到相同的客户端 ID,则代理会启动客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM