Exemplo n.º 1
0
 //异步关闭
 public void CloseClient()
 {
     try
     {
         mqttClient.DisconnectAsync();
     }
     catch (Exception)
     {
         throw;
     }
 }
Exemplo n.º 2
0
 private static async Task DisConnectMqttServerAsync()
 {
     try
     {
         await mqttClient.DisconnectAsync();
     }
     catch (Exception ex)
     {
         LogHelper.Log(ex.ToString());
     }
 }
Exemplo n.º 3
0
 public override void Dispose()
 {
     if (_mqttClient != null)
     {
         if (_mqttClient.IsConnected)
         {
             var task = new Task(() => { _mqttClient.DisconnectAsync(); });
             task.RunSynchronously();
         }
         _mqttClient.Dispose();
     }
 }
Exemplo n.º 4
0
        public async void DisconnectAsyncCallsSocketSendMessageAsyncWithParams()
        {
            var socketMock = new Mock <ISocketAdapter>();
            var loggerMock = new Mock <ILogger>();
            var client     = new MqttClient(socketMock.Object, loggerMock.Object, SocketEncryption.None);

            await client.DisconnectAsync();

            socketMock.Verify(socket => socket.WriteAsync(
                                  It.Is <SocketEventArgs>(a => a != null && a.MessageToSend.MessageType == MessageType.Disconnect)),
                              Times.Once());
        }
Exemplo n.º 5
0
 /// <summary>
 /// Disconnects from the MQTT server.
 /// </summary>
 public MqttClientHelper Disconnect()
 {
     if (mqttClient != null)
     {
         if (mqttClient.IsConnected)
         {
             mqttClient.DisconnectAsync();
         }
         mqttClient.Dispose();
         mqttClient = null;
     }
     return(this);
 }
        protected override async Task ClientStop()
        {
            try
            {
                if (mqttClient == null)
                {
                    return;
                }
                await mqttClient.DisconnectAsync();

                mqttClient = null;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"客户端尝试断开Server出错.>{ex.Message}");
            }
        }
Exemplo n.º 7
0
 public void DisConnect()
 {
     if (this.mqttClient != null)
     {
         try
         {
             Task task = mqttClient.DisconnectAsync();
             this.IsConnected = false;
             task.Wait(5000);
             this.RemoveEvents();
             mqttClient.Dispose();
             mqttClient = null;
         }
         catch
         {
         }
     }
 }
Exemplo n.º 8
0
        private async Task ClientStop()
        {
            try
            {
                if (mqttClient == null)
                {
                    return;
                }
                await mqttClient.DisconnectAsync();

                mqttClient = null;
            }
            catch (Exception ex)
            {
                lbxMonitor.BeginInvoke(_updateMonitorAction,
                                       Logger.TraceLog(Logger.Level.Fatal, $"客户端尝试断开Server出错.>{ex.Message}"));
            }
        }
Exemplo n.º 9
0
        private async Task Disconnect()
        {
            if (_Client != null)
            {
                _Client.Reconnect = false;
                await _Client.DisconnectAsync();

                _Client.Dispose();
                _Client = null;

                "关闭连接".SpeechTip();
            }

            gbSetting.Enabled   = true;
            gbSubscribe.Enabled = false;
            gbSend.Enabled      = false;
            btnConnect.Text     = "连接";
        }
Exemplo n.º 10
0
        public void Stop()
        {
            //移除守护定时器
            _daemonTimer?.Dispose();
            _daemonTimer = null;

            _publishHelper.Stop();
            _publishHelper.OnPublishMessage -= OnPublishMessage;

            try
            {
                //退出时,断开连接
                _mqttClient?.DisconnectAsync();
                _mqttClient?.Dispose();
                _mqttClient = null;
            }
            catch (Exception e)
            {
                OnErrorMessage?.Invoke(e.Message);
            }
        }
Exemplo n.º 11
0
        public async Task DisposeAsync()
        {
            if (appContext != null)
            {
                await appContext
                .DisposeAsync()
                .ConfigureAwait(false);
            }

            await MqttClient
            .DisconnectAsync()
            .ConfigureAwait(false);

            if (mqttServer != null)
            {
                await mqttServer
                .StopAsync()
                .ConfigureAwait(false);
            }

            LogoHardwareMock?.Dispose();
        }
Exemplo n.º 12
0
        public static async Task SendToServer(string topic, string payload)
        {
            int connectTryCount = 0;

            while (connectTryCount < 2 && !s_client.IsConnected)
            {
                if (!_loggedNotConnected)
                {
                    Dbg.Write("MQTTPublish - Client NotConnected");
                    _loggedNotConnected = true;
                }

                await Connect().ConfigureAwait(false);

                connectTryCount++;
                if (!s_client.IsConnected)
                {
                    Task.Delay(1000 * 2);
                }
            }

            if (s_client.IsConnected == false)
            {
                if (!_loggedNotConnected)
                {
                    Dbg.Write("MQTTPublish - Client NotConnected");
                    _loggedNotConnected = true;
                }
            }
            else
            {
                _loggedNotConnected = false;
                _loggedError        = false;

                var message = new MqttApplicationMessageBuilder()
                              .WithTopic(topic)
                              .WithPayload(payload)
                              .WithExactlyOnceQoS()
                              .WithRetainFlag()
                              .Build();


                try
                {
                    await s_client.PublishAsync(message).ConfigureAwait(false);

                    Dbg.Write("MQTT Message Sent");
                }
                catch (Exception ex)
                {
                    Dbg.Write("MQTTPublish - Error publishing message: " + ex.Message);
                    try
                    {
                        if (s_client.IsConnected)
                        {
                            await s_client.DisconnectAsync();
                        }
                    }
                    catch (Exception)
                    {
                        Dbg.Trace("MQTTPublish - Exception closing connection after publish error - client still connected");
                        // Expected if the publish failed (but, probably not if the client still thinks it is connected).
                    }
                }
            }
        }
Exemplo n.º 13
0
 public void Dispose()
 {
     MqttClient.DisconnectAsync().Wait();
     MqttClient.Dispose();
 }
Exemplo n.º 14
0
 private void button_Disconnect_Click(object sender, EventArgs e)
 {
     isReconnect = false;
     Task.Run(async() => { await mqttClient.DisconnectAsync(); });
 }
Exemplo n.º 15
0
 public async Task DisconnectAsync()
 {
     await _mqttClient.DisconnectAsync();
 }