Created
March 27, 2020 23:01
-
-
Save ldoguin/274f1aed6e3acb5d247c231401e19057 to your computer and use it in GitHub Desktop.
run a vertx http server on 8080 and an mqtt server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GrabResolver(name='staging', root ='https://oss.sonatype.org/content/repositories/iovertx-3863/') | |
@Grab(group='io.vertx', module='vertx-core', version='3.8.5') | |
@Grab(group='io.vertx', module='vertx-mqtt', version='3.8.5') | |
@Grab(group='io.vertx', module='vertx-web', version='3.8.5') | |
@Grab(group='io.vertx', module='vertx-lang-groovy', version='3.8.5') | |
import io.vertx.mqtt.MqttServer | |
import io.vertx.mqtt.MqttServerOptions | |
import io.vertx.core.AbstractVerticle; | |
import io.vertx.core.DeploymentOptions; | |
import io.vertx.core.Vertx; | |
import io.vertx.core.VertxOptions; | |
import io.vertx.ext.web.Router | |
class Launcher { | |
public static void main(String[] args) throws Exception { | |
Vertx vertx = Vertx.vertx(); | |
vertx.deployVerticle(new MyMqttServer()); | |
vertx.deployVerticle(new HttpServer()); | |
} | |
} | |
class MyMqttServer extends AbstractVerticle { | |
@Override | |
public void start() throws Exception { | |
MqttServerOptions options = new MqttServerOptions(); | |
options.setPort(4040); | |
MqttServer mqttServer = MqttServer.create(vertx, options); | |
mqttServer.endpointHandler({endpoint -> | |
// shows main connect info | |
System.out.println("MQTT client [" + endpoint.clientIdentifier() + "] request to connect, clean session = " + endpoint.isCleanSession()); | |
if (endpoint.auth() != null) { | |
System.out.println("[username = " + endpoint.auth().getUsername() + ", password = " + endpoint.auth().getPassword() + "]"); | |
} | |
if (endpoint.will() != null) { | |
System.out.println("[will topic = " + endpoint.will().getWillTopic() + " msg = " + new String(endpoint.will().getWillMessageBytes()) + | |
" QoS = " + endpoint.will().getWillQos() + " isRetain = " + endpoint.will().isWillRetain() + "]"); | |
} | |
System.out.println("[keep alive timeout = " + endpoint.keepAliveTimeSeconds() + "]"); | |
// accept connection from the remote client | |
endpoint.accept(false); | |
}) | |
.listen({ar -> | |
if (ar.succeeded()) { | |
System.out.println("MQTT server is listening on port " + ar.result().actualPort()); | |
} else { | |
System.out.println("Error on starting the server"); | |
ar.cause().printStackTrace(); | |
} | |
}); | |
} | |
} | |
class HttpServer extends AbstractVerticle { | |
@Override | |
public void start() throws Exception { | |
Router router = Router.router(vertx) | |
router.route().handler({ routingContext -> | |
routingContext.response().putHeader("content-type", "text/html").end("Hello World!") | |
}) | |
vertx.createHttpServer().requestHandler(router.&accept).listen(8080) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment