无法连接到MQTT服务器:“原因代码:5-无权执行请求的操作”

时间:2019-10-11 12:50:21

标签: java server mqtt message paho

  • 任务:将“ Hello world”发送到服务器进行测试。
  • 服务器:clouldMQTT。
  • 库:PahoMQTT。
  • 原因码:5-无权执行请求的操作。
  • 原因:空。
  • 消息代码:无连接权限。
  • 代码:

    String topic    = "Lesson_MQTT_server";
    String broker   = "tcp://m15.cloudmqtt.com:18594";
    char [] pass    = "qmaoaTfDebag".toCharArray(); // Fake pass
    String userId   = "oelmanhl";
    String content  = "Hello world!";
    int qos         = 0;
    
    
    try {
        MqttClient client = new MqttClient (broker, userId, new MemoryPersistence());
        MqttConnectOptions options = new MqttConnectOptions ();
        options.setAutomaticReconnect(true);
        options.setCleanSession(true);
        options.setPassword(pass);          
        System.out.println ("Connecting..."); 
    
        client.connect(options); 
        System.out.println ("Conntected!");
    
        MqttMessage message = new MqttMessage ();
        message.clearPayload();
        message.setPayload(content.getBytes());
        message.setQos(qos);
        System.out.println ("Publishing message...");
    
        client.publish(topic, message);
        System.out.println ("Message published!");
    
        client.disconnect();
        System.out.println ("Disconect!");
    
    } catch (MqttException ex) {
        System.out.println ("Reason code: " + ex.getReasonCode());
        System.out.println ("Cause code: " + ex.getCause());
        System.out.println ("Message code: " + ex.getMessage());
    }
    

0 个答案:

没有答案