localhost导致MQTT本地客户端与本地broker无法连接问题

今天在调试时发现一个localhost问题:

#define EMBER_AF_PLUGIN_TRANSPORT_MQTT_BROKER_ADDRESS "tcp://localhost:1883"
#define EMBER_AF_PLUGIN_TRANSPORT_MQTT_BROKER_ADDRESS_LENGTH (20)

当MQTT客户端设置本地地址为localhost:1883时,无法连接本地broker.

#define EMBER_AF_PLUGIN_TRANSPORT_MQTT_BROKER_ADDRESS "tcp://127.0.0.1:1883"
#define EMBER_AF_PLUGIN_TRANSPORT_MQTT_BROKER_ADDRESS_LENGTH (20)

修改localhost为127.0.0.1后问题解决

这里分析原因:

root@sun8i:/etc# ls
adb_profile      etc_complete     modules-boot.d   rc.d
banner           fstab            modules.d        res
banner.failsafe  group            mtab             resolv.conf
camera.cfg       hotplug.d        openwrt_release  shadow
cedarx.conf      init.d           openwrt_version  shells
config           inittab          opkg             ts.conf
crontabs         localtime        opkg.conf        tslib-env.sh
device_info      lost+found       passwd
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Spring Integration 从 MQTT 客户端获取客户端连接状态的完整代码示例: ```java import org.eclipse.paho.client.mqttv3.IMqttAsyncClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.integration.annotation.ServiceActivator; import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory; import org.springframework.integration.mqtt.core.MqttPahoClientFactory; import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter; import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; @Configuration public class MqttClientConfiguration { private static final String CLIENT_ID = "mqttClientId"; private static final String BROKER_URL = "tcp://localhost:1883"; private static final String TOPIC = "test/topic"; private static final int QOS = 1; @Bean public MqttPahoClientFactory mqttClientFactory() { DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); factory.setServerURIs(BROKER_URL); return factory; } @Bean public MessageChannel mqttInputChannel() { return new DirectChannel(); } @Bean public MqttPahoMessageDrivenChannelAdapter mqttInbound() { MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(CLIENT_ID, mqttClientFactory(), TOPIC); adapter.setCompletionTimeout(5000); adapter.setConverter(new DefaultPahoMessageConverter()); adapter.setQos(QOS); adapter.setOutputChannel(mqttInputChannel()); return adapter; } @ServiceActivator(inputChannel = "mqttInputChannel") public void handleMqttMessage(Message<byte[]> message) { // 处理 MQTT 消息 } @Bean public IMqttAsyncClient mqttAsyncClient() throws Exception { IMqttAsyncClient mqttAsyncClient = mqttClientFactory().createMqttAsyncClient(BROKER_URL, CLIENT_ID); mqttAsyncClient.connect().waitForCompletion(); return mqttAsyncClient; } @Bean public boolean mqttClientConnected() throws Exception { return mqttAsyncClient().isConnected(); } } ``` 在这个示例中,我们使用 Spring Integration 提供的 `MqttPahoMessageDrivenChannelAdapter` 类来创建一个 MQTT 客户端,并将其连接到指定的 MQTT 服务器。我们还使用 `DirectChannel` 定义了一个消息通道,以便我们可以处理从 MQTT 服务器接收到的消息。我们还定义了一个 `handleMqttMessage` 方法来处理消息。 我们还定义了一个 `mqttAsyncClient` 方法来创建一个 MQTT 异步客户端,并将其连接到指定的 MQTT 服务器。在这个方法中,我们使用了 `mqttClientFactory()` 方法来创建一个 `MqttPahoClientFactory` 对象,这是一个 Spring Integration 提供的 MQTT 客户端工厂类。 最后,我们定义了一个 `mqttClientConnected` 方法来检查 MQTT 客户端是否连接MQTT 服务器。在这个方法中,我们使用了 `mqttAsyncClient()` 方法来获取 MQTT 异步客户端,并使用 `isConnected()` 方法来检查客户端是否连接到服务器。 需要注意的是,这个示例中的 MQTT 客户端连接代码并没有在 Spring Bean 生命周期中启动。你需要在你的应用程序中调用 `mqttAsyncClient()` 方法来启动 MQTT 客户端连接
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值