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; mqttSyncClient = new MqttSyncClient(options); OperateResult connect = await mqttSyncClient.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); } }
private async void button7_Click_1(object sender, EventArgs e) { // 连接 MqttConnectionOptions options = new MqttConnectionOptions( ) { IpAddress = textBox4.Text, Port = int.Parse(textBox2.Text), ClientId = textBox1.Text, UseRSAProvider = checkBox_rsa.Checked, }; if (!string.IsNullOrEmpty(textBox9.Text) || !string.IsNullOrEmpty(textBox10.Text)) { options.Credentials = new MqttCredential(textBox9.Text, textBox10.Text); } button7.Enabled = false; mqttSyncClient = new MqttSyncClient(options); OperateResult connect = await mqttSyncClient.ConnectServerAsync( ); if (connect.IsSuccess) { // 此处为什么关闭呢?因为文件的操作都是基于新的socket的,这样支持多线程操作,所以此处关闭也没事 // Why is it closed here? Because file operations are based on the new socket, this supports multi-threaded operations, so it’s okay to close here mqttSyncClient.ConnectClose( ); panel2.Enabled = true; button7.Enabled = false; button1.Enabled = true; MessageBox.Show(StringResources.Language.ConnectServerSuccess); } else { button7.Enabled = true; MessageBox.Show(connect.Message); } // 创建本地文件存储的路径 string path = Application.StartupPath + @"\Files"; if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } }