Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

39
LINES

< > BotCompany Repo | #1026859 // MQTT Subscriber Spike

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1103K of libraries. Click here for Pure Java version (1908L/11K).

1  
!7
2  
3  
lib 1400324 // Eclipse Paho (MQTT)
4  
5  
import org.eclipse.paho.client.mqttv3.*;
6  
7  
cmodule MQTTSubscriberSpike > DynPrintLog {
8  
  switchable S topic = "iot_data";
9  
  transient MqttClient client;
10  
  
11  
  start-thread {
12  
    client = new MqttClient("tcp://localhost:1883", MqttClient.generateClientId());
13  
    client.setCallback(new MyCallback);
14  
    client.connect();
15  
    client.subscribe(topic);
16  
    print("Connected & subscribed to " + topic);
17  
  }
18  
  
19  
  void cleanMeUp_mqtt ctex {
20  
    if (client != null) {
21  
      client.disconnect();
22  
      client.close();
23  
    }
24  
  }
25  
  
26  
  class MyCallback implements MqttCallback {
27  
    public void connectionLost(Throwable throwable) enter {
28  
      print("Connection to MQTT broker lost!");
29  
    }
30  
31  
    public void messageArrived(S s, MqttMessage mqttMessage) enter {
32  
      print("Message received: "+ fromUtf8(mqttMessage.getPayload()));
33  
    }
34  
35  
    public void deliveryComplete(IMqttDeliveryToken token) enter {
36  
      print("Delivery complete: " + token);
37  
    }
38  
  }
39  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1026859
Snippet name: MQTT Subscriber Spike
Eternal ID of this version: #1026859/8
Text MD5: 5f010e47ba3611f85d8919d5813fc385
Transpilation MD5: 14eae3d161fb3b9c325bfdbc72a1eb2a
Author: stefan
Category: javax / mqtt
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-01-29 14:36:43
Source code size: 1029 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 264 / 1359
Version history: 7 change(s)
Referenced in: [show references]