我正在尝试使用 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
提交
取消
