// region MqttActionCallBack -- connection success or fail @Override public void onSuccess(IMqttToken iMqttToken) { isConnected = true; ShowToast("连接成功"); try { client.subscribe("Likaci/MqttMap", 0); } catch (MqttException e) { e.printStackTrace(); } }
@Override protected void init(VaadinRequest request) { System.out.println("request = " + request); final VerticalLayout layout = new VerticalLayout(); layout.setMargin(true); setContent(layout); layout.addComponent(chart); try { empf = new MqttClient("tcp://192.168.0.106:1883", "MyfirstMQTTEmpf", new MemoryPersistence()); empf.setCallback( new MqttCallback() { @Override public void connectionLost(Throwable throwable) {} @Override public void messageArrived(String str, MqttMessage mqttMessage) throws Exception { byte[] payload = mqttMessage.getPayload(); lastMessage = new String(payload); System.out.println("s = " + str + " msg " + lastMessage); } @Override public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {} }); Button button = new Button("refresh"); button.addClickListener( event -> access( () -> chart .getConfiguration() .getSeries() .forEach( s -> ((ListSeries) s) .updatePoint(0, Double.valueOf(lastMessage.split(":")[3]))))); layout.addComponent(button); empf.connect(); empf.subscribe(TOPIC, 1); mqqtThread.start(); } catch (MqttException e) { e.printStackTrace(); } }