Example #1
0
        private void WorkerThreadProc()
        {
            try
            {
                var connArgs = new MqttConnectionArgs()
                {
                    Hostname        = _connectionInfo.Connection.Server,
                    Port            = _connectionInfo.Connection.Port,
                    Secure          = false,
                    ProtocolVersion = MqttProtocolVersion.V3_1_1,
                    ClientId        = _connectionInfo.Connection.ConnectionUser,
                    Username        = _connectionInfo.Connection.ConnectionUser,
                    Password        = _connectionInfo.Connection.Password,
                    CleanSession    = true,
                    Keepalive       = TimeSpan.FromSeconds(60),
                    WillMessage     = null,
                    ReadTimeout     = TimeSpan.FromSeconds(10),
                    WriteTimeout    = TimeSpan.FromSeconds(10)
                };
                using (var conn = new MqttConnection(connArgs))
                {
                    conn.Connect();

                    conn.PublishReceived += HandlePublishReceived;

                    if (_connectionInfo.Subscriptions != null)
                    {
                        foreach (var s in _connectionInfo.Subscriptions)
                        {
                            logger.Info($"Subscribe: {Key} -> {s.TopicFilter}, {s.QoS}");
                            conn.Subscribe(s.TopicFilter, (MqttQos)s.QoS);
                        }
                    }

                    while (conn.Loop())
                    {
                        // etc...
                    }

                    logger.Warn("Disconnected: " + Key);
                }
            }
            catch (ThreadAbortException)
            {
                logger.Info("Aborted: " + Key);
            }
            catch (Exception ex)
            {
                logger.Error("Error in WorkerProc", ex);
            }
        }
Example #2
0
 void Subscribe(MqttConnection conn)
 {
     conn.Subscribe(TopicToSubscribe, Qos);
 }