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 void button1_Click(object sender, EventArgs e) { panel2.Enabled = true; // 连接 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); } mqttClient = new MqttClient(options); mqttClient.LogNet = new HslCommunication.LogNet.LogNetSingle(string.Empty); mqttClient.LogNet.BeforeSaveToFile += LogNet_BeforeSaveToFile; OperateResult connect = mqttClient.ConnectServer( ); if (connect.IsSuccess) { button1.Enabled = false; button2.Enabled = true; panel2.Enabled = true; MessageBox.Show(StringResources.Language.ConnectServerSuccess); } else { MessageBox.Show(StringResources.Language.ConnectedFailed + connect.ToMessageShowString( )); } }
/// <summary> /// Sets the options according to the controls. /// </summary> private void ControlsToOptions() { // connection MqttConnectionOptions connectionOptions = options.ConnectionOptions; connectionOptions.Server = txtServer.Text; connectionOptions.Port = Convert.ToInt32(numPort.Value); connectionOptions.ClientID = txtClientID.Text; connectionOptions.Username = txtUsername.Text; connectionOptions.Password = txtPassword.Text; connectionOptions.Timeout = Convert.ToInt32(numTimeout.Value); connectionOptions.ProtocolVersion = (MqttProtocolVersion)cbProtocolVersion.SelectedIndex; // publishing PublishOptions publishOptions = options.PublishOptions; publishOptions.RootTopic = txtRootTopic.Text; publishOptions.UndefinedValue = txtUndefinedValue.Text; publishOptions.PublishFormat = txtPublishFormat.Text; publishOptions.QosLevel = cbQosLevel.SelectedIndex; publishOptions.Retain = chkRetain.Checked; publishOptions.MaxQueueSize = Convert.ToInt32(numMaxQueueSize.Value); publishOptions.DataLifetime = Convert.ToInt32(numDataLifetime.Value); publishOptions.DetailedLog = publishOptions.DetailedLog; publishOptions.DeviceFilter.Clear(); publishOptions.DeviceFilter.AddRange(ScadaUtils.ParseRange(txtDeviceFilter.Text, true, true)); options.AddToOptionList(dataSourceConfig.CustomOptions); }
/// <summary> /// Sets the controls according to the options. /// </summary> private void OptionsToControls() { // connection MqttConnectionOptions connectionOptions = options.ConnectionOptions; txtServer.Text = connectionOptions.Server; numPort.SetValue(connectionOptions.Port); txtClientID.Text = connectionOptions.ClientID; txtUsername.Text = connectionOptions.Username; txtPassword.Text = connectionOptions.Password; numTimeout.SetValue(connectionOptions.Timeout); cbProtocolVersion.SelectedIndex = (int)connectionOptions.ProtocolVersion; // publishing PublishOptions publishOptions = options.PublishOptions; txtRootTopic.Text = publishOptions.RootTopic; txtUndefinedValue.Text = publishOptions.UndefinedValue; txtPublishFormat.Text = publishOptions.PublishFormat; cbQosLevel.SelectedIndex = publishOptions.QosLevel; chkRetain.Checked = publishOptions.Retain; numMaxQueueSize.SetValue(publishOptions.MaxQueueSize); numDataLifetime.SetValue(publishOptions.DataLifetime); chkDetailedLog.Checked = publishOptions.DetailedLog; txtDeviceFilter.Text = publishOptions.DeviceFilter.ToRangeString(); }
private bool stopReading; // indicates that a read operation should stop /// <summary> /// Initializes a new instance of the class. /// </summary> public TopicReader(MqttConnectionOptions mqttConnectionOptions, RichTextBoxHelper logHelper) { connOptions = mqttConnectionOptions ?? throw new ArgumentNullException(nameof(mqttConnectionOptions)); this.logHelper = logHelper ?? throw new ArgumentNullException(nameof(logHelper)); mqttLock = new object(); tokenSource = null; messageDT = DateTime.MinValue; stopReading = false; ReadResult = false; WirenBoardModel = new WirenBoardModel(); }
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); } }
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); } }
/// <summary> /// Initializes a new instance of the class. /// </summary> public MqttDSO(OptionList options) { ConnectionOptions = new MqttConnectionOptions(options); PublishOptions = new PublishOptions(options); }
/// <summary> /// Initializes a new instance of the class. /// </summary> public FrmMqttClientChannelOptions(ChannelConfig channelConfig) : this() { this.channelConfig = channelConfig ?? throw new ArgumentNullException(nameof(channelConfig)); options = new MqttConnectionOptions(channelConfig.CustomOptions); }