/// <summary>
    /// The method that handles the button click to start the server.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The event args.</param>
    private async void ButtonServerStart_Click(object sender, EventArgs e)
    {
        if (this.mqttServer != null)
        {
            return;
        }

        var storage = new JsonServerStorage();

        storage.Clear();

        this.mqttServer = new MqttFactory().CreateMqttServer();

        var options = new MqttServerOptions();

        options.DefaultEndpointOptions.Port = int.Parse(this.TextBoxPort.Text);
        options.Storage = storage;
        options.EnablePersistentSessions = true;

        try
        {
            await this.mqttServer.StartAsync(options);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error Occurs", MessageBoxButtons.OK, MessageBoxIcon.Error);
            await this.mqttServer.StopAsync();

            this.mqttServer = null;
        }
    }
Пример #2
0
    /// <summary>
    /// The method that handles the button click to start the server.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The event args.</param>
    private async void ButtonServerStartClick(object sender, EventArgs e)
    {
        if (this.mqttServer != null)
        {
            return;
        }

        var storage = new JsonServerStorage();

        storage.Clear();
        this.mqttServer = new MqttFactory().CreateMqttServer();
        var options = new MqttServerOptions();

        options.DefaultEndpointOptions.Port = int.Parse(this.TextBoxPort.Text);
        options.Storage = storage;
        options.EnablePersistentSessions = true;
        options.ConnectionValidator      = new MqttServerConnectionValidatorDelegate(
            c =>
        {
            if (c.ClientId.Length < 10)
            {
                c.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid;
                return;
            }

            if (c.Username != "username")
            {
                c.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
                return;
            }

            if (c.Password != "password")
            {
                c.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
                return;
            }

            c.ReasonCode = MqttConnectReasonCode.Success;
        });

        try
        {
            await this.mqttServer.StartAsync(options);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error Occurs", MessageBoxButtons.OK, MessageBoxIcon.Error);
            await this.mqttServer.StopAsync();

            this.mqttServer = null;
        }
    }