@Override public void start() throws Exception { MqttServerOptions options = new MqttServerOptions() .setPort(1883) .setHost("0.0.0.0"); MqttServer server = MqttServer.create(vertx, options); server.endpointHandler(endpoint -> { System.out.println("connected client " + endpoint.clientIdentifier()); endpoint.publishHandler(message -> { System.out.println("Just received message on [" + message.topicName() + "] payload [" + message.payload() + "] with QoS [" + message.qosLevel() + "]"); }); endpoint.accept(false); }); server.listen(ar -> { if (ar.succeeded()) { System.out.println("MQTT server started and listening on port " + server.actualPort()); } else { System.err.println("MQTT server error on start" + ar.cause().getMessage());
@Override public void start() throws Exception { MqttClientOptions options = new MqttClientOptions(); options.setSsl(true); options.setTrustAll(true); MqttClient mqttClient = MqttClient.create(vertx, options); mqttClient.connect(BROKER_PORT, BROKER_HOST, ch -> { if (ch.succeeded()) { System.out.println("Connected to a server"); mqttClient.publish( MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE), MqttQoS.AT_MOST_ONCE, false, false, s -> mqttClient.disconnect(d -> System.out.println("Disconnected from server"))); } else { System.out.println("Failed to connect to a server"); System.out.println(ch.cause()); } }); }
@Override public void start() throws Exception { MqttServerOptions options = new MqttServerOptions() .setPort(8883) .setPemKeyCertOptions(new PemKeyCertOptions() .setKeyPath("server-key.pem") .setCertPath("server-cert.pem")) .setSsl(true); MqttServer mqttServer = MqttServer.create(vertx, options); .endpointHandler(endpoint -> { System.out.println("MQTT client [" + endpoint.clientIdentifier() + "] request to connect, " + "clean session = " + endpoint.isCleanSession()); endpoint.accept(false); .listen(ar -> { System.out.println("MQTT server is listening on port " + mqttServer.actualPort()); } else { System.err.println("Error on starting the server" + ar.cause().getMessage());
MqttServer mqttServer = MqttServer.create(vertx); .endpointHandler(endpoint -> { System.out.println("MQTT client [" + endpoint.clientIdentifier() + "] request to connect, clean session = " + endpoint.isCleanSession()); if (endpoint.auth() != null) { System.out.println("[username = " + endpoint.auth().userName() + ", password = " + endpoint.auth().password() + "]"); if (endpoint.will() != null) { System.out.println("[will flag = " + endpoint.will().isWillFlag() + " topic = " + endpoint.will().willTopic() + " msg = " + endpoint.will().willMessage() + " QoS = " + endpoint.will().willQos() + " isRetain = " + endpoint.will().isWillRetain() + "]"); System.out.println("[keep alive timeout = " + endpoint.keepAliveTimeSeconds() + "]"); endpoint.accept(false); endpoint.subscribeHandler(subscribe -> { for (MqttTopicSubscription s : subscribe.topicSubscriptions()) { System.out.println("Subscription for " + s.topicName() + " with QoS " + s.qualityOfService()); grantedQosLevels.add(s.qualityOfService()); endpoint.subscribeAcknowledge(subscribe.messageId(), grantedQosLevels); endpoint.publish(subscribe.topicSubscriptions().get(0).topicName(), Buffer.buffer("Hello from the Vert.x MQTT server"), subscribe.topicSubscriptions().get(0).qualityOfService(), false,
@Override public void start() throws Exception { MqttClientOptions options = new MqttClientOptions().setKeepAliveTimeSeconds(2); MqttClient client = MqttClient.create(Vertx.vertx(), options); client.publishHandler(publish -> { System.out.println("Just received message on [" + publish.topicName() + "] payload [" + publish.payload().toString(Charset.defaultCharset()) + "] with QoS [" + publish.qosLevel() + "]"); }); client.subscribeCompletionHandler(h -> { System.out.println("Receive SUBACK from server with granted QoS : " + h.grantedQoSLevels()); client.publish( MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE), vertx.setTimer(5000, l -> client.unsubscribe(MQTT_TOPIC)); }); client.unsubscribeCompletionHandler(h -> { System.out.println("Receive UNSUBACK from server"); vertx.setTimer(5000, l -> client.disconnect(d -> System.out.println("Disconnected form server")) ); }); client.connect(BROKER_PORT, BROKER_HOST, ch -> { if (ch.succeeded()) {
@Override public void start() throws Exception { MqttClient mqttClient = MqttClient.create(vertx); mqttClient.connect(BROKER_PORT, BROKER_HOST, ch -> { if (ch.succeeded()) { System.out.println("Connected to a server"); mqttClient.publish( MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE), MqttQoS.AT_MOST_ONCE, false, false, s -> mqttClient.disconnect(d -> System.out.println("Disconnected from server"))); } else { System.out.println("Failed to connect to a server"); System.out.println(ch.cause()); } }); } }
/** * @return the Will information as provided by the remote MQTT client */ public MqttWill will() { if (cached_4 != null) { return cached_4; } MqttWill ret = delegate.will(); cached_4 = ret; return ret; }
/** * Set a close handler. This will be called when the MQTT endpoint is closed * @param handler the handler * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.mqtt.MqttEndpoint closeHandler(Handler<Void> handler) { delegate.closeHandler(handler); return this; }
/** * Sends the PUBACK message to the remote MQTT client * @param publishMessageId identifier of the PUBLISH message to acknowledge * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.mqtt.MqttEndpoint publishAcknowledge(int publishMessageId) { delegate.publishAcknowledge(publishMessageId); return this; }
/** * Close the server. * <p> * The close happens asynchronously and the server may not be closed until some time after the call has returned. */ public void close() { delegate.close(); }
/** * Set a disconnect handler on the MQTT endpoint. This handler is called when a DISCONNECT * message is received by the remote MQTT client * @param handler the handler * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.mqtt.MqttEndpoint disconnectHandler(Handler<Void> handler) { delegate.disconnectHandler(handler); return this; }
/** * Sends the PUBCOMP message to the remote MQTT client * @param publishMessageId identifier of the PUBLISH message to acknowledge * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.mqtt.MqttEndpoint publishComplete(int publishMessageId) { delegate.publishComplete(publishMessageId); return this; }
/** * Subscribes to the topics with related QoS levels * @param topics topics and related QoS levels to subscribe to * @return current MQTT client instance */ public io.vertx.rxjava.mqtt.MqttClient subscribe(Map<String, Integer> topics) { delegate.subscribe(topics); return this; }
/** * Sets handler which will be called after UNSUBACK packet receiving * @param unsubscribeCompletionHandler handler to call with the packetid * @return current MQTT client instance */ public io.vertx.rxjava.mqtt.MqttClient unsubscribeCompletionHandler(Handler<Integer> unsubscribeCompletionHandler) { delegate.unsubscribeCompletionHandler(unsubscribeCompletionHandler); return this; }
/** * Set an exception handler. This will be called when an error at protocol level happens * @param handler the handler * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.mqtt.MqttEndpoint exceptionHandler(Handler<Throwable> handler) { delegate.exceptionHandler(handler); return this; }
/** * Disconnects from the MQTT server calling disconnectHandler after disconnection * @param disconnectHandler handler called when asynchronous disconnect call ends * @return current MQTT client instance */ public io.vertx.rxjava.mqtt.MqttClient disconnect(Handler<AsyncResult<Void>> disconnectHandler) { delegate.disconnect(disconnectHandler); return this; }
/** * Unsubscribe from receiving messages on given topic * @param topic Topic you want to unsubscribe from * @param unsubscribeSentHandler handler called after UNSUBSCRIBE packet sent * @return current MQTT client instance */ public io.vertx.rxjava.mqtt.MqttClient unsubscribe(String topic, Handler<AsyncResult<Integer>> unsubscribeSentHandler) { delegate.unsubscribe(topic, unsubscribeSentHandler); return this; }
/** * Enable/disable publishing (in/out) auto acknowledge * @param isPublishAutoAck auto acknowledge status * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.mqtt.MqttEndpoint publishAutoAck(boolean isPublishAutoAck) { delegate.publishAutoAck(isPublishAutoAck); return this; }
/** * @return auto acknowledge status for publishing (in/out) */ public boolean isPublishAutoAck() { boolean ret = delegate.isPublishAutoAck(); return ret; }
/** * Subscribes to the topic and adds a handler which will be called after the request is sent * @param topics topics you subscribe on * @param subscribeSentHandler handler called after SUBSCRIBE packet sent with packetid * @return current MQTT client instance */ public io.vertx.rxjava.mqtt.MqttClient subscribe(Map<String, Integer> topics, Handler<AsyncResult<Integer>> subscribeSentHandler) { delegate.subscribe(topics, subscribeSentHandler); return this; }