1

i have this Mqtt ckient app that am working on, the publish method is working fine but am having a hard time with the subscribe method.

this is the subscribe method, am suppose to click a button to subcribe to a topic:

 public void subscribe( MqttClient MC) {

 String topic = jTextField3.getText();
 int qos = jComboBox1.getSelectedIndex() ;

 String[] topics = {topic};

 int[] QoS = {qos};

 if ( jLabel3.getText().equals("Connected") ) {

    try {

    MC.subscribe( topics, QoS );

          System.out.println(topics +"    "+QoS);
                 System.out.println(topic +"    "+qos);
            jButton2.setText("Subscribed");
            jTextField4.setText(topics.toString());

        } catch ( Exception ex ) {
   JOptionPane.showMessageDialog( this, ex.getMessage(), 
    "MQTT Subscription Exception", JOptionPane.ERROR_MESSAGE );
        }   
    } else {
    jTextArea1.setText("Not connected");
    }       
}   

this is the actionPerformed method for the button

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    subscribe(MC);

}

this is my publishedArrived method:

  public void publishArrived( String topic, byte[] data, int QoS, boolean        retained ) {
    jTextField4.setText(topic);
   String msgData = new String(data);
  jTextArea1.setText(new String(data));
    }

someone should please help me out here.

1 Answers1

0

You need to implement a callback and a message arrived function. This function will handle the message. See paho mqtt message client as example in here Subscribe and Read MQTT Message Using PAHO

Community
  • 1
  • 1
kamils
  • 1
  • 1