private void Btn_connect_Click(object sender, EventArgs e) { if (mqttNetClient.IsConnectionSuccess) { if (this.tb_hostname.Text != mqttNetClient.ServerUrl) { this.tb_hostname.Text = ""; this.tb_hostname.Focus(); return; } if (this.tb_port.Text != mqttNetClient.Port.ToString()) { this.tb_port.Text = ""; this.tb_port.Focus(); return; } if (this.tb_username.Text != mqttNetClient.UserID) { this.tb_username.Text = ""; this.tb_username.Focus(); return; } if (this.tb_password.Text != mqttNetClient.PassWord) { this.tb_password.Text = ""; this.tb_password.Focus(); return; } } mqttNetClient.ServerUrl = this.tb_hostname.Text; int port; if (!int.TryParse(this.tb_port.Text, out port)) { MessageBox.Show("端口号格式错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } mqttNetClient.Port = port; mqttNetClient.UserID = this.tb_username.Text; mqttNetClient.PassWord = this.tb_password.Text; mqttNetClient.ServerName = this.tb_connectName.Text; mqttNetClient.StartConnection(mqttNetClient); }
private void Btn_connect_Click(object sender, EventArgs e) { if (mqttNetClient.IsConnectionSuccess) { if (this.tb_hostname.Text != mqttNetClient.ServerUrl) { this.tb_hostname.Text = ""; this.tb_hostname.Focus(); return; } if (this.tb_port.Text != mqttNetClient.Port.ToString()) { this.tb_port.Text = ""; this.tb_port.Focus(); return; } if (this.tb_username.Text != mqttNetClient.UserID) { this.tb_username.Text = ""; this.tb_username.Focus(); return; } if (this.tb_password.Text != mqttNetClient.PassWord) { this.tb_password.Text = ""; this.tb_password.Focus(); return; } } mqttNetClient.ServerUrl = this.tb_hostname.Text; int port; if (!int.TryParse(this.tb_port.Text, out port)) { MessageBox.Show("端口号格式错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } mqttNetClient.Port = port; mqttNetClient.UserID = this.tb_username.Text; mqttNetClient.PassWord = this.tb_password.Text; mqttNetClient.StartConnection(mqttNetClient); while (true) { if (mqttNetClient.IsConnectionSuccess) { if (MessageBox.Show("是否进入订阅主题页面?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) != DialogResult.OK) { this.Close(); } else { Broker broker = new Broker(mqttNetClient); broker.ShowDialog(); } timer.Enabled = false; waitConnectTime = 0; timer.Stop(); break; } else { timer.Start(); if (waitConnectTime == 5) { timer.Enabled = false; waitConnectTime = 0; timer.Stop(); break; } } } }