使用M2Mqtt连接公共MQTT Broker失败求助(.NET Core 3.1)
问题描述
我尝试用C#连接多个公共MQTT Broker均失败。按照EMQX官方文档编写代码,基于.NET Core 3.1框架与M2Mqtt 4.3.0 NuGet包,出现错误提示:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
测试过的公共Broker包括broker.emqx.io、broker.hivemq.com、mqtt.eclipse.org、test.mosquitto.org,即使移除认证凭证仍无法连接。有没有开发者成功用C#连接过公共MQTT Broker?求解决方案或建议。
带凭证测试代码
using System; using uPLibrary.Networking.M2Mqtt; using uPLibrary.Networking.M2Mqtt.Messages; namespace csharpMQTT { class Program { static MqttClient ConnectMQTT(string broker, int port, string clientId, string username, string password) { MqttClient client = new MqttClient(broker, port, false, MqttSslProtocols.None, null, null); client.Connect(clientId, username, password); if (client.IsConnected) { Console.WriteLine("Connected to MQTT Broker"); } else { Console.WriteLine("Failed to connect"); } return client; } static void Main(string[] args) { string broker = "broker.emqx.io"; int port = 1883; string clientId = Guid.NewGuid().ToString(); string username = "emqx"; string password = "public"; MqttClient client = ConnectMQTT(broker, port, clientId, username, password); } } }
无凭证测试代码
using System; using uPLibrary.Networking.M2Mqtt; using uPLibrary.Networking.M2Mqtt.Messages; namespace csharpMQTT { class Program { static MqttClient ConnectMQTT(string broker, int port, string clientId) { MqttClient client = new MqttClient(broker, port, false, MqttSslProtocols.None, null, null); client.Connect(clientId); if (client.IsConnected) { Console.WriteLine("Connected to MQTT Broker"); } else { Console.WriteLine("Failed to connect"); } return client; } static void Main(string[] args) { string broker = "broker.hivemq.com"; int port = 1883; string clientId = Guid.NewGuid().ToString(); MqttClient client = ConnectMQTT(broker, port, clientId); } } }
解决方案与建议
1. 排查网络环境
- 先用
ping broker.emqx.io或telnet broker.emqx.io 1883测试网络连通性,多数连接失败是因为防火墙、公司网络代理或运营商屏蔽了1883端口。 - 切换手机热点测试,排除本地网络限制;若公司网络拦截,尝试使用SSL端口(如8883)连接。
2. 更换MQTT客户端库
M2Mqtt是较老旧的库,对.NET Core兼容性有限。推荐使用MQTTnet——目前维护活跃、支持全.NET平台的主流MQTT客户端库。
MQTTnet示例代码
using System; using System.Threading.Tasks; using MQTTnet; using MQTTnet.Client; namespace csharpMQTT { class Program { static async Task Main(string[] args) { var factory = new MqttFactory(); var mqttClient = factory.CreateMqttClient(); var options = new MqttClientOptionsBuilder() .WithTcpServer("broker.emqx.io", 1883) .WithCredentials("emqx", "public") .WithClientId(Guid.NewGuid().ToString()) .Build(); try { await mqttClient.ConnectAsync(options); Console.WriteLine("Connected to MQTT Broker"); } catch (Exception ex) { Console.WriteLine($"连接失败:{ex.Message}"); } } } }
使用前需通过NuGet安装MQTTnet包。
3. 调整M2Mqtt连接参数(若坚持使用原库)
- 设置更长的连接超时:
client.ConnectTimeout = 10000;(10秒),避免因默认超时过短导致失败。 - 尝试切换到SSL端口(如8883),修改代码中
port参数并将MqttSslProtocols.None改为对应SSL协议(如MqttSslProtocols.TLSv1_2)。
4. 验证基础配置
- 确保客户端ID唯一(用
Guid.NewGuid().ToString()生成是可靠的)。 - 确认认证信息正确:比如broker.emqx.io的默认凭证是
emqx/public,无凭证Broker需调用无参Connect方法。
内容的提问来源于stack exchange,提问作者bags
相关产品推荐
相关产品推荐

