Untitled

 avatar
unknown
plain_text
2 years ago
766 B
3
Indexable
String brokerUrl = "tcp:192.168.1.10:1883";
String clientId = "myClientId";
MqttClient mqttClient = new MqttClient(brokerUrl, clientId);
mqttClient.connect();
String topic = "my/topic";
mqttClient.subscribe(topic);
mqttClient.setCallback(new MqttCallback() {
    public void messageArrived(String topic, MqttMessage message) {
        String payload = new String(message.getPayload());
        System.out.println("Received message on topic " + topic + ": " + payload);
        // TODO: Store the message in a database or file
    }

    public void connectionLost(Throwable cause) {
        System.out.println("Connection lost: " + cause.getMessage());
    }

    public void deliveryComplete(IMqttDeliveryToken token) {
        // Not used in this example
    }
});
Editor is loading...