Esempio n. 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);
            }
        }
Esempio n. 2
0
        public void Run()
        {
            var connArgs = new MqttConnectionArgs()
            {
                ClientId        = this.ClientId,
                Hostname        = "localhost",
                CleanSession    = false,
                ProtocolVersion = MqttProtocolVersion.V3_1
            };

            using (var conn = new MqttConnection(connArgs, clientPersistence))
            {
                conn.Connect();
                Console.WriteLine("{0} connected", ClientId);
                try
                {
                    BindEvents(conn);

                    if (!conn.IsSessionPresent)
                    {
                        Subscribe(conn);
                    }

                    while (conn.Loop(PollLimit))
                    {
                        if (Finished && !conn.IsPublishing)
                        {
                            break;
                        }

                        bool finishedPublishing = previousPublishedNumber >= maxNumber;
                        bool canPublishNext     = numbersPersistence.GetLastReceived(TopicToPublish) >= previousPublishedNumber;

                        if (!conn.IsPublishing && !finishedPublishing && canPublishNext)
                        {
                            PublishNext(conn);
                        }
                    }
                }
                finally
                {
                    UnbindEvents(conn);
                }
            }
        }
Esempio n. 3
0
        public void Run()
        {
            var connArgs = new MqttConnectionArgs()
            {
                ClientId     = this.ClientId,
                Hostname     = "localhost",
                Port         = 1883,
                Secure       = true,
                CleanSession = false
            };

            using (var conn = new MqttConnection(connArgs, persistence))
            {
                conn.Connect();
                Console.WriteLine("{0} connected", ClientId);

                try
                {
                    BindEvents(conn);

                    if (!conn.IsSessionPresent)
                    {
                        Subscribe(conn);
                    }

                    while (conn.Loop(PollLimit) && !Finished)
                    {
                        if (!conn.IsPublishing)
                        {
                            PublishNext(conn);
                        }
                    }
                }
                finally
                {
                    UnbindEvents(conn);
                }
            }
        }