package com.muyu.web.controller;

import com.muyu.common.core.utils.uuid.UUID;
import org.eclipse.paho.client.mqttv3.*;

public class testController {
    public static void main(String[] args) {
        String broker       = "tcp://47.101.53.251:1883";
        String clientId     = "SX-"+ UUID.randomUUID().toString();
        MqttClient client;

        try {
            client = new MqttClient(broker, clientId);
            MqttConnectOptions connectOptions = new MqttConnectOptions();
            connectOptions.setCleanSession(true);
            System.out.println("Connect to broker:"+broker);
            client.connect(connectOptions);
            System.out.println("Connected");
            client.setCallback(new MqttCallback() {
                @Override
                public void connectionLost(Throwable throwable) {
                    System.out.println("连接丢失");
                }

                @Override
                public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
                    System.out.println("消息到达:"+new String(mqttMessage.getPayload())+topic);
                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

                }
            });
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }
    }

}
