我正在嘗試使用 org.eclipse.paho.client.mqttv3-1.2.0 中的軟件包實現 Pub-Sub.jar在用戶代碼下方:import org.eclipse.paho.client.mqttv3.MqttClient;import org.eclipse.paho.client.mqttv3.MqttTopic;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 java.net.*;public class Subscriber { //public static final String BROKER_URL = "tcp://iot.eclipse.org:1883"; public static final String BROKER_URL = "tcp://localhost:1883"; private MqttClient client; String topic = "Demo Topic"; public static void main(String[] args) { System.out.println("MQTT Broker: " + BROKER_URL); new Subscriber(); } public Subscriber() { String clientId = getMacAddress() + "-sub"; System.out.println("Client ID = " + clientId); try { client = new MqttClient(BROKER_URL, clientId); client.connect(); client.setCallback(new SubscribeCallback()); client.subscribe(topic); } catch (MqttException e) { e.printStackTrace(); System.exit(1); } } public byte[] getMacAddress(){ byte[] mac = new byte[6]; try{ InetAddress address = InetAddress.getLocalHost(); NetworkInterface nwi = NetworkInterface.getByInetAddress(address); mac = nwi.getHardwareAddress(); System.out.println(mac); } catch(Exception e) { System.out.println(e); } return mac; }}class SubscribeCallback implements MqttCallback { @Override public void connectionLost (Throwable cause) { } @Override public void messageArrived (String topic, MqttMessage message) { System.out.println("Message arrived. Topic: " + topic + " Message: " + message.toString()); if ("I'm gone".equals(topic)) { System.out.println("Sensor gone!"); } } @Override public void deliveryComplete (IMqttDeliveryToken token) { }
1 回答

隔江千里
TA貢獻1906條經驗 獲得超10個贊
您還需要將當前目錄添加到類路徑中
java -cp .:org.eclipse.paho.client.mqttv3-1.2.0.jar Subscriber
如果在窗口上運行,則更改 for。:
;
添加回答
舉報
0/150
提交
取消