Exemple #1
0
        private void CreateMQChannel()
        {
            if (m_mqttClient.IsPublishing)
            {
                return;
            }
            StopMQTTChannel();

            try
            {
                var connArgs = new MqttConnectionArgs()
                {
                    ClientId  = m_clientId,
                    Hostname  = m_mqttServerAddress,
                    Port      = m_mqttPort,
                    Keepalive = new TimeSpan(1, 0, 0)
                };

                m_mqttClient = new MqttConnection(connArgs, m_mqttPersistence, null);

                m_mqttClient.Connect();
            }
            catch (Exception ex)
            {
                string text = "创建MQTTChannel失败," + ex.Message;
                m_eventLogger.WriteWarning(text);
                USeNotifyEventArgs notify = new USeNotifyEventArgs(USeNotifyLevel.Warning, text);
                SafeRaiseNotifyEvent(this, notify);
            }
        }
        /// <summary>
        /// 读数据线程
        /// </summary>
        private void DoWork()
        {
            lock (m_locker)
            {
                try
                {
                    var connArgs = new MqttConnectionArgs()
                    {
                        ClientId  = m_clientId,
                        Hostname  = m_mqttServerAddress,
                        Port      = m_mqttPort,
                        Keepalive = new TimeSpan(1, 0, 0)
                    };

                    using (m_mqttClient = new MqttConnection(connArgs))
                    {
                        m_mqttClient.Connect();

                        while (m_runFlag)
                        {
                            USeMarketData marketData = null;
                            m_marketDataQueue.TryDequeue(out marketData);
                            if (marketData == null)
                            {
                                Thread.Sleep(1000);
                                continue;
                            }

                            Debug.WriteLine(string.Format("当前MQTT链接:{0}", connArgs.ClientId));

                            //[hanyu]暂时只推送上期的品种行情
                            if (marketData.Instrument.Market == USeMarket.SHFE || marketData.Instrument.Market == USeMarket.LME)
                            {
                                InternalSendTotMQTT(marketData);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);

                    string text = string.Format("** {0}链接MQTT失败,{1}", this.StoreageName, ex.Message);
                    m_eventLogger.WriteError(text);
                    USeNotifyEventArgs notify = new USeNotifyEventArgs(USeNotifyLevel.Warning, text);
                    SafeRaiseNotifyEvent(this, notify);
                }
                finally
                {
                    m_mqttClient.Disconnect();
                    DoWork();
                }
            }
        }
Exemple #3
0
        public void ReloadApp()
        {
            var wasConnected = MqttConnection.IsConnected;

            MqttConnection.Connect();

            // Reinitialize workers only if client was connected to the server before
            if (wasConnected)
            {
                MqttConnection.InitializeWorkers();
            }
        }
Exemple #4
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);
            }
        }
Exemple #5
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);
                }
            }
        }
Exemple #6
0
 private void MainForm_Shown(object sender, EventArgs e)
 {
     if (Utils.Settings.MqttServer.Length > 3)
     {
         MqttConnection.Connect();
         if (MqttConnection.IsConnected)
         {
             toolStripStatusLabel1.Text = "connected to " + Utils.Settings.MqttServer;
         }
         else
         {
             toolStripStatusLabel1.Text = "not connected";
         }
     }
     else
     {
         toolStripStatusLabel1.Text = "not connected";
         OpenSettings();
     }
 }
Exemple #7
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);
                }
            }
        }