我正在尝试在 Windows 上配置 Mosquitto MQTT 代理,并使用用户名和密码进行身份验证。
我在 mosquitto.conf 文件中添加了以下行:
允许匿名假
用户名测试remote_username测试
远程密码测试123
运行代理时出现以下错误:
C:\Program Files (x86)\mosquitto>mosquitto.exe -c mosquitto.conf -p 1883 错误:桥接配置无效。在 mosquitto.conf 发现错误:750。错误:无法打开配置文件。
无需用户名和密码,我就可以启动代理并与 MQTTLens 客户端连接。
谢谢,阿肖克
我面临特定 client_id 断开连接的问题。当我尝试连接时,它会突然断开连接并显示错误
1450599983: New connection from 192.168.2.245 on port 1883.
1450599983: Client 46144 disconnected.
1450599983: New client connected from 192.168.2.245 as 46144 (c0, k60).
1450599984: Socket error on client 46144, disconnecting.
Run Code Online (Sandbox Code Playgroud)
我的conf.d文件是
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
cleansession true
Run Code Online (Sandbox Code Playgroud)
有时会在发送消息后发生。为什么会发生这种情况,我们该如何解决这个问题?
谢谢
我是 MQTT 消息系统的新手,但我设法在运行 AOSP kk-4.4.2 的 snapdragon 板上实现了 paho Android 服务。我的服务将在启动时启动,并将通过端口 1883 连接到 HiveMQ 的公共代理。
问题是在将运行自定义 AOSP 的 Android 设备连接到服务器之后,如果我尝试从手机上运行的 Android 应用程序连接另一个客户端。骁龙板中的客户端自动与服务器断开连接。当我尝试重新连接时,它再次连接到服务器。
我在连接到服务器时使用不同的客户端 ID 和用户名
我正在研究 MQTT 协议,并尝试使用 2 个不同的 Java 应用程序进行发布和订阅。
我的第一个应用程序是“发布”。我在 MQTT 服务器上发布一条消息。
我的第二个应用程序是“订阅”。我正在订阅一个主题并尝试接收消息。但我从来没有收到消息。
当我运行这两个应用程序时,我从“订阅”应用程序开始,然后运行“发布”应用程序。当“发布”应用程序启动时,我失去了与“订阅”应用程序的连接,并且无法接收消息。
在“订阅”应用程序中,我的方法 messageArrived() 永远不会被 client.setCallback(this) 调用。(参见下面的代码)。
这是我的 2 个代码应用程序:
发布应用程序:PubClient 类:
package publishclient;
import java.util.Random;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class PubClient implements MqttCallback {
MqttClient client;
MqttConnectOptions connOpt;
Random rand = new Random();
int nbRandom = 0;
int valMax =151, valMin = 40;
public PubClient() throws MqttException {
String broker = "tcp://88.177.147.17:1883"; // Adress MQTT Server …
Run Code Online (Sandbox Code Playgroud) 我尝试在 MQTT Broker(ubuntu 服务器)上执行此代码,
当从发布者获取数据时将发送到 UDP_IP。
我的代码:
import json
import socket
import paho.mqtt.client as mqtt
# MQTT Settings
MQTT_Broker = "192.168.0.106"
MQTT_Topic = "#"
MQTT_Port = 1883
Keep_Alive_Interval = 45
UDP_IP = "192.168.0.108"
UDP_RAW_PORT = 5567
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
def on_connect(mosq, obj, rc):
mqttc.subscribe(MQTT_Topic, 0)
def on_message(mosq, obj, msg):
gps_handler(msg.topic, msg.payload)
def gps_handler(Topic, jsonData):
jsonData = jsonData[1:-1]
sock.sendto(jsonData, (UDP_IP, UDP_RAW_PORT))
print jsonData
def on_subscribe(mosq, obj, mid, granted_qos):
pass
mqttc = mqtt.Client()
# Assign event callbacks
mqttc.on_message = on_message
mqttc.on_connect = …
Run Code Online (Sandbox Code Playgroud) 我想在nodejs中开发一个发布者->订阅者模型,其中有1个发布者和许多订阅者。
目前我的想法是使用普通的 websocket。这样做的问题是,如果通过互联网运行,每个订阅者都需要静态 IP和端口转发。这不符合要求。
这个问题的解决方案似乎是 MQTT,因为它应该适合该用例,但我看到它也运行在 websockets 上,这应该会导致相同的问题,还是 MQTT 处理它的方式不同?
本质上,我需要一个解决方案,其中发布者拥有静态 IP,而订阅者可以位于世界任何地方。这可以通过 MQTT 实现吗?还是我需要其他解决方案?
我正在尝试使用 NodeJS 连接到 MQTT 服务器,但它根本无法连接。这是我正在使用的代码:
const mqtt = require('mqtt');
function Connect(serverName,serverUsername,serverPassword,port,topic,clientName) {
try{
//const client = new mqtt.connect('mqtt://'+serverName);
const client = new mqtt.connect('mqtts://'+serverName,{rejectUnauthorized:false,username:serverUsername,password:serverPassword,connectTimeout:5000});
console.log('--connecting');
client.on('connect',function(){
console.log('--connected');
client.subscribe(topic,function(err){
console.log('--subscribed');
if (!err) {
client.publish(topic,"hello 2");
}
});
});
client.on('message',function(tp,msg){
console.log(msg.toString());
client.end();
});
} catch(e) {
console.log(e);
}
}
Connect('mqtt.example.com','myusername','mypassword',9101,'test','NodeApp');
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,console.log
内部的任何语句都不会client.subscribe
触发。我使用这些命令测试了我的 MQTT 服务器,我的所有订阅和发布的消息都工作正常:
mosquitto_sub -h mqtt.example.com -p 9101 -t "test" -u "myusername" -P "mypassword" --capath /etc/ssl/certs/
mosquitto_pub -h mqtt.example.com -p 9101 -t "test" -m "This is my message" -u …
Run Code Online (Sandbox Code Playgroud) 我的 raspberrypi 上的以下 python 代码没有连接到我的 mqtt 代理,它只是在打印后挂起Connecting...
:
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("test")
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client(client_id="",clean_session=True,userdata=None,protocol=mqtt.MQTTv311,transport="tcp")
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(username="stackoverflow",password="stackoverflow")
print("Connecting...")
client.connect("learn.evermight.net", 9101, 10)
client.loop_forever()
Run Code Online (Sandbox Code Playgroud)
我的 Python 代码做错了什么?
命令行成功
我确认我的 mqtt 确实有效,因为我可以使用以下命令从终端订阅:
mosquitto_sub -h learn.evermight.net -p 9101 -t "test" -u "stackoverflow" -P "stackoverflow" --capath /etc/ssl/certs/
Run Code Online (Sandbox Code Playgroud)
一旦我从另一个终端运行此命令,我就会在终端中看到消息
mosquitto_pub -h learn.evermight.net -p 9101 -t "test" -u "stackoverflow" -P "stackoverflow" -m "hello world" …
Run Code Online (Sandbox Code Playgroud) 我使用以下代码进行 Mqtt 客户端:基于证书的身份验证,用于在 VMware 中运行的 MQTT 代理。我正在通过 Windows 系统中的代码运行 Mqtt 客户端。
public class TestClient {
public static void main(String[] args) {
String serverUrl = "ssl://192.168.5.12:8883";
String caFilePath = "rootca.pem";
String clientCrtFilePath = "Client.pem";
String clientKeyFilePath = "Client.key";
String mqttUserName = "guest";
String mqttPassword = "123123";
MqttClient client;
try {
client = new MqttClient(serverUrl, "2");
MqttConnectOptions options = new MqttConnectOptions();
// options.setUserName(mqttUserName);
// options.setPassword(mqttPassword.toCharArray());
// options.setSSLProperties(props);
options.setConnectionTimeout(0);
options.setKeepAliveInterval(300);
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
SSLSocketFactory socketFactory = getSocketFactory(caFilePath,
clientCrtFilePath, clientKeyFilePath, "");
options.setSocketFactory(socketFactory);
System.out.println("starting connect the server..."); …
Run Code Online (Sandbox Code Playgroud) 当尝试连接到本地运行的 Mosquitto MQTT 队列时,出现以下错误。
Unhandled exception. System.AggregateException: One or more errors occurred. (The client has been disconnected while trying to perform the connection)
---> System.Net.Mqtt.MqttClientException: The client has been disconnected while trying to perform the connection
at System.Net.Mqtt.Sdk.MqttClientImpl.ConnectAsync(MqttClientCredentials credentials, MqttLastWill will, Boolean cleanSession)
Run Code Online (Sandbox Code Playgroud)
我在设置时使用默认选项System.Net.Mqtt.MqttClient
。
Unhandled exception. System.AggregateException: One or more errors occurred. (The client has been disconnected while trying to perform the connection)
---> System.Net.Mqtt.MqttClientException: The client has been disconnected while trying to perform the connection
at System.Net.Mqtt.Sdk.MqttClientImpl.ConnectAsync(MqttClientCredentials …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Qos=2 发布消息,保留 message=false 和 clean session=true。如果我的订户离线,则再次在线时不会收到 Qos 2 消息。但是发布者成功拿到了 PUBREC 和 PUBCOMP
我正在使用spring-integration-mqtt
依赖项编写 Spring Boot 应用程序,其中我使用CommandLineRunner
beanMQTTSubscriber
在应用程序启动时启动。
但是,当我运行该应用程序时,出现以下错误:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-01-26 01:48:40.386 ERROR 59171 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field MessageListener in im.sma.mqtt.mqttclient.DemoApplication required a single bean, but 2 were found:
- messageListener: defined in file [/Users/sma/sandbox/slidecab/mqtt/mqtt-client/target/classes/im/sma/mqtt/mqttclient/config/MessageListener.class]
- integrationHeaderChannelRegistry: defined in null
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, …
Run Code Online (Sandbox Code Playgroud)