use of io.vertx.mqtt.MqttClientOptions in project vertx-examples by vert-x3.
the class Client method start.
@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());
}
});
}
use of io.vertx.mqtt.MqttClientOptions in project hono by eclipse.
the class MqttTestBase method connectToAdapter.
/**
* Opens a connection to the MQTT adapter using an X.509 client certificate.
*
* @param cert The client certificate to use for authentication.
* @param hostname The name of the host to connect to.
* @return A future that will be completed with the CONNACK packet received
* from the adapter or failed with a {@link io.vertx.mqtt.MqttConnectionException}
* if the connection could not be established.
* @throws NullPointerException if any of the parameters are {@code null}.
*/
protected final Future<MqttConnAckMessage> connectToAdapter(final SelfSignedCertificate cert, final String hostname) {
Objects.requireNonNull(cert);
Objects.requireNonNull(hostname);
final Promise<MqttConnAckMessage> result = Promise.promise();
vertx.runOnContext(connect -> {
final MqttClientOptions options = new MqttClientOptions(defaultOptions);
options.setKeyCertOptions(cert.keyCertOptions());
mqttClient = MqttClient.create(vertx, options);
mqttClient.connect(IntegrationTestSupport.MQTTS_PORT, hostname, result);
});
return result.future().map(conAck -> {
LOGGER.info("MQTTS connection to adapter [host: {}, port: {}] established", hostname, IntegrationTestSupport.MQTTS_PORT);
this.context = Vertx.currentContext();
return conAck;
});
}
use of io.vertx.mqtt.MqttClientOptions in project hono by eclipse.
the class MqttTestBase method connectToAdapter.
/**
* Opens a connection to the MQTT adapter using given credentials.
*
* @param tlsVersion The TLS protocol version to use for connecting to the adapter.
* @param username The username to use for authentication.
* @param password The password to use for authentication.
* @return A future that will be completed with the CONNACK packet received
* from the adapter or failed with a {@link io.vertx.mqtt.MqttConnectionException}
* if the connection could not be established.
*/
protected final Future<MqttConnAckMessage> connectToAdapter(final String tlsVersion, final String username, final String password) {
final Promise<MqttConnAckMessage> result = Promise.promise();
vertx.runOnContext(connect -> {
final MqttClientOptions options = new MqttClientOptions(defaultOptions).setUsername(username).setPassword(password);
options.setEnabledSecureTransportProtocols(Set.of(tlsVersion));
mqttClient = MqttClient.create(vertx, options);
mqttClient.connect(IntegrationTestSupport.MQTTS_PORT, IntegrationTestSupport.MQTT_HOST, result);
});
return result.future().map(conAck -> {
LOGGER.info("MQTTS connection to adapter [host: {}, port: {}] established", IntegrationTestSupport.MQTT_HOST, IntegrationTestSupport.MQTTS_PORT);
this.context = Vertx.currentContext();
return conAck;
});
}
Aggregations