Working with vert.x core 3.2.1, I saw this configuration of a server using a web socket:

vertx.createHttpServer().websocketHandler(ws -> ws.handler(ws::writeBinaryMessage)).requestHandler(req -> { if (req.uri().equals("/")) req.response().sendFile("ws.html"); }).listen(8080); 

By recording, I realized that on the vertex platform we are creating an http server which, using the listen method, listening to port 8080, is waiting for a possible connection to it. It also uses a web-based handler, which is associated with the request-handler. But there is no clear understanding of what is happening in websocketHandler. Lambda expressions and :: - operator confused me. Please explain what executes this code, or if possible, write this construction without lambda.

    1 answer 1

    After trial and error, it seems to have managed to bring this spell with lambdas to a simpler and cumbersome look:

     server.websocketHandler(ws -> ws.handler(ws::writeBinaryMessage)) .requestHandler(req -> { if (req.uri().equals("smile")) req.response().end("hello");}); server.websocketHandler(new Handler<ServerWebSocket>() { @Override public void handle(ServerWebSocket webs) { webs.writeBinaryMessage(Buffer.buffer()); } }).requestHandler(new Handler<HttpServerRequest>() { @Override public void handle(HttpServerRequest req) { if (req.uri().equals("smile")) req.response().end("hello"); } }); 

    As a result, we see the following: the first handler in this tutorial works with a web socket connection and allows you to exchange messages, the second handler is designed to work with http and displays "hello" in the browser. Thus, the vertex provides a set of interfaces (handlers) that allow you to work with connections of various types.