package com.muyu.data.util;

import org.eclipse.paho.client.mqttv3.*;

public class ReceiveTwo {

    public static void main(String[] args) {
        String topic = "vehicle";
        String broker = "tcp://47.101.53.251:1883";
        String clientId="lw";

        try {
            MqttClient mqttClient= new MqttClient(broker,clientId);
            MqttConnectOptions connectOptions=new MqttConnectOptions();
            connectOptions.setCleanSession(true);
            System.out.println("Connecting to broker：" + broker);
            mqttClient.connect(connectOptions);
            System.out.println("已连接");
            mqttClient.setCallback(new MqttCallback(){

                @Override
                public void connectionLost(Throwable throwable) {
                    System.out.println("Connect lost!");
                }

                @Override
                public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
                    System.out.println("Message arrived. topic:"+topic);
                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

                }
            });

            mqttClient.subscribe(topic);
            System.out.println("Subscribed to topic " + topic);
        } catch (MqttException e) {
            System.out.println("reason "+e.getReasonCode());
            System.out.println("msg " +e.getMessage());
            System.out.println("loc " +e.getLocalizedMessage());
            System.out.println("cause "+e.getCause());
            System.out.println("excep "+e);
            e.printStackTrace();
        }


    }



}
