我正在构建一个用于通过MQTT发布数据的C#应用程序。应允许用户选择是否要使用SSL。 1883年的MQTT连接工作正常。但我遇到了试图通过端口8883实现的问题。我对MqttClient类重载方法感到困惑。我遵循了link。
MqttClient client = new MqttClient("broker.hivemq.com",
MqttSettings.MQTT_BROKER_DEFAULT_SSL_PORT,
true,
new X509Certificate(Resources.m2mqtt_ca));
但是当我尝试实现相同的操作时,我遇到了构建错误
Error 1 'uPLibrary.Networking.M2Mqtt.MqttClient' does not contain a constructor that takes 4 arguments.
所以我决定让下一个字段与我的ca证书保持一致,并为SSL选择一个随机版本。以下是我的客户端代码。
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
client = new MqttClient(ddlServerIP.Text,
MqttSettings.MQTT_BROKER_DEFAULT_SSL_PORT,
true,
new X509Certificate("C:/openSSLcertificate/m2mqtt_ca.crt",new X509Certificate("C:/openSSLcertificate/m2mqtt_ca.crt"),
MqttSslProtocols.TLSv1_0);
String clientId= Guid.NewGuid().ToString();
byte code = client.Connect(clientId);
switch (code)
{
case 0:
MessageBox.Show("Connection Accepted with return code " + code);
Simulation frmSim = new Simulation();
frmSim.FormClosed += new FormClosedEventHandler(Simulation_FormClosed);
frmSim.Show();
BEConnect frmBeConnect = new BEConnect();
FormProvider.MainMenu.Hide();
break;
case 1:
MessageBox.Show("Connection Refused with return code " + code + Environment.NewLine + " unacceptable protocol version");
break;
case 2:
MessageBox.Show("Connection Refused with return code " + code + Environment.NewLine + " identifier rejected");
break;
case 3:
MessageBox.Show("Connection Refused with return code " + code + Environment.NewLine + " Server unavailable");
break;
case 4:
MessageBox.Show("Connection Refused with return code " + code + Environment.NewLine + " bad user name or password");
break;
case 5:
MessageBox.Show("Connection Refused with return code " + code + Environment.NewLine + " not authorized");
break;
default:
MessageBox.Show("Connection Failed");
break;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我还对我的mosquitto.conf文件进行了以下更改,该文件在我的服务器端Raspberry Pi上运行。我最终还是得到了一个连接到经纪人的例外。
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
port 1883
listener 8883
cafile /etc/mosquitto/ca_certificates/m2mqtt_ca.crt
certfile /etc/mosquitto/certs/m2mqtt_srv.crt
keyfile /etc/mosquitto/certs/m2mqtt_srv.key
require_certificate false
Mosquitto日志如下:
1510146433: New connection from 192.168.0.9 on port 8883.
1510146433: OpenSSL Error: error:1408A0C1:SSL routines:SSL3_GET_CLIENT_HELLO:no shared cipher
1510146433: Socket error on client <unknown>, disconnecting