示例#1
0
        private async void StartServer(object sender, RoutedEventArgs e)
        {
            if (_mqttServer != null)
            {
                return;
            }

            JsonServerStorage storage = null;

            if (ServerPersistRetainedMessages.IsChecked == true)
            {
                storage = new JsonServerStorage();

                if (ServerClearRetainedMessages.IsChecked == true)
                {
                    storage.Clear();
                }
            }

            _mqttServer = new MqttFactory().CreateMqttServer();

            var options = new MqttServerOptions();

            options.DefaultEndpointOptions.Port = int.Parse(ServerPort.Text);
            options.Storage = storage;

            await _mqttServer.StartAsync(options);
        }
示例#2
0
        private async void StartServer(object sender, RoutedEventArgs e)
        {
            if (!hasStarted)
            {
                hasStarted = true;
                TimeSpan        period        = TimeSpan.FromSeconds(1);
                ThreadPoolTimer PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer(async(source) =>
                {
                    await Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                    {
                        titsy = NetScans();

                        if (titsy == "None")
                        {
                            Rings.IsActive = true;
                            try { if (!connacking)
                                  {
                                      try { Connack(); } catch (Exception eee) { }
                                  }
                            }
                            catch (Exception ee) { }
                        }
                        else
                        {
                            Rings.IsActive = false;
                        }
                    });
                }, period);

                if (_mqttServer != null)
                {
                    return;
                }

                startedd.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/StartedButton2.png"));
                Slips.Fill           = startedd;
                JsonServerStorage storage = null;
                storage = new JsonServerStorage();
                storage.Clear();

                _mqttServer = new MqttFactory().CreateMqttServer();

                var options = new MqttServerOptions();
                options.DefaultEndpointOptions.Port = 1883;
                options.Storage = storage;

                await _mqttServer.StartAsync(options);

                var tlsOptions = new MqttClientTlsOptions
                {
                    UseTls = false,
                    IgnoreCertificateChainErrors      = true,
                    IgnoreCertificateRevocationErrors = true,
                    AllowUntrustedCertificates        = true
                };

                var options2 = new MqttClientOptions {
                    ClientId = ""
                };


                options2.ChannelOptions = new MqttClientTcpOptions
                {
                    Server     = machiner,
                    Port       = 1883,
                    TlsOptions = tlsOptions
                };

                if (options2.ChannelOptions == null)
                {
                    throw new InvalidOperationException();
                }

                /*options.Credentials = new MqttClientCredentials
                 * {
                 *  Username = User.Text,
                 *  Password = Password.Text
                 * };*/

                options2.CleanSession    = true;
                options2.KeepAlivePeriod = TimeSpan.FromSeconds(5);

                try
                {
                    if (_mqttClient != null)
                    {
                        await _mqttClient.DisconnectAsync();

                        _mqttClient.ApplicationMessageReceived -= OnApplicationMessageReceived;
                    }

                    var factory = new MqttFactory();
                    _mqttClient = factory.CreateMqttClient();
                    _mqttClient.ApplicationMessageReceived += OnApplicationMessageReceived;

                    await _mqttClient.ConnectAsync(options2);
                }
                catch (Exception exception)
                {
                    //Trace.Text += exception + Environment.NewLine;
                }

                if (_mqttClient == null)
                {
                    return;
                }



                Slips.Fill = startedd;
                var qos = MqttQualityOfServiceLevel.ExactlyOnce;
                await _mqttClient.SubscribeAsync(new TopicFilter("luxProto2", qos));
            }
        }