private void button2_Click(object sender, EventArgs e) { // 断开连接 button5.Enabled = true; button1.Enabled = true; button2.Enabled = false; panel2.Enabled = false; mqttClient.ConnectClose( ); }
private async void button1_Click(object sender, EventArgs e) { // 连接 MqttConnectionOptions options = new MqttConnectionOptions( ) { IpAddress = textBox1.Text, Port = int.Parse(textBox2.Text), ClientId = textBox3.Text, KeepAlivePeriod = TimeSpan.FromSeconds(int.Parse(textBox6.Text)), UseRSAProvider = checkBox_rsa.Checked, }; if (!string.IsNullOrEmpty(textBox9.Text) || !string.IsNullOrEmpty(textBox10.Text)) { options.Credentials = new MqttCredential(textBox9.Text, textBox10.Text); } button1.Enabled = false; mqttClient?.ConnectClose( ); mqttClient = new MqttClient(options); mqttClient.LogNet = new HslCommunication.LogNet.LogNetSingle(string.Empty); mqttClient.LogNet.BeforeSaveToFile += LogNet_BeforeSaveToFile; mqttClient.OnMqttMessageReceived += MqttClient_OnMqttMessageReceived; //mqttClient.OnNetworkError += MqttClient_OnNetworkError; // 自己来控制异常及重连的操作 OperateResult connect = await mqttClient.ConnectServerAsync( ); if (connect.IsSuccess) { panel2.Enabled = true; button1.Enabled = false; button2.Enabled = true; panel2.Enabled = true; MessageBox.Show(StringResources.Language.ConnectServerSuccess); } else { mqttClient = null; button1.Enabled = true; MessageBox.Show(connect.ToMessageShowString( )); } }
private async void button1_Click(object sender, EventArgs e) { // 连接 MqttConnectionOptions options = new MqttConnectionOptions( ) { IpAddress = textBox1.Text, Port = int.Parse(textBox2.Text), ClientId = textBox3.Text, }; if (!string.IsNullOrEmpty(textBox9.Text) || !string.IsNullOrEmpty(textBox10.Text)) { options.Credentials = new MqttCredential(textBox9.Text, textBox10.Text); } button1.Enabled = false; mqttClient?.ConnectClose( ); mqttClient = new MqttClient(options); mqttClient.LogNet = new HslCommunication.LogNet.LogNetSingle(string.Empty); mqttClient.LogNet.BeforeSaveToFile += LogNet_BeforeSaveToFile; mqttClient.OnMqttMessageReceived += MqttClient_OnMqttMessageReceived; mqttClient.OnNetworkError += MqttClient_OnNetworkError; OperateResult connect = await mqttClient.ConnectServerAsync( ); if (connect.IsSuccess) { panel2.Enabled = true; button1.Enabled = false; button2.Enabled = true; panel2.Enabled = true; MessageBox.Show(StringResources.Language.ConnectServerSuccess); } else { button1.Enabled = true; MessageBox.Show(connect.Message); } }
public void Dispose() { client.ConnectClose(); }