コード例 #1
0
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Message.Content = string.Empty;

                if (this.receptor != null)
                {
                    this.receptor.Dispose();
                    this.receptor = null;
                }

                if (this.mqtt != null)
                {
                    this.mqtt.Dispose();
                    this.mqtt = null;
                }

                this.ConnectButton.IsEnabled = true;
                this.Host.IsEnabled          = true;
                this.Port.IsEnabled          = true;
                this.UserName.IsEnabled      = true;
                this.Password.IsEnabled      = true;
                this.Topic.IsEnabled         = true;
                this.Tls.IsEnabled           = true;
                this.Trust.IsEnabled         = true;
                this.CloseButton.IsEnabled   = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
コード例 #2
0
        private void ConnectButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Message.Content = string.Empty;

                if (!int.TryParse(this.Port.Text, out int Port))
                {
                    throw new Exception("Invalid port number.");
                }

                this.mqtt = new MqttClient(this.Host.Text, Port, this.Tls.IsChecked.HasValue && this.Tls.IsChecked.Value,
                                           this.UserName.Text, this.Password.Password)
                {
                    TrustServer = this.Trust.IsChecked.HasValue && this.Trust.IsChecked.Value,
                };

                this.mqtt.OnStateChanged    += Mqtt_OnStateChanged;
                this.mqtt.OnConnectionError += Mqtt_OnConnectionError;
                this.mqtt.OnError           += Mqtt_OnError;
                this.mqtt.OnSubscribed      += Mqtt_OnSubscribed;

                this.receptor          = new MqttEventReceptor(this.mqtt);
                this.receptor.OnEvent += Receptor_OnEvent;

                this.ConnectButton.IsEnabled = false;
                this.Host.IsEnabled          = false;
                this.Port.IsEnabled          = false;
                this.UserName.IsEnabled      = false;
                this.Password.IsEnabled      = false;
                this.Topic.IsEnabled         = false;
                this.Tls.IsEnabled           = false;
                this.Trust.IsEnabled         = false;
                this.CloseButton.IsEnabled   = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }