Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I estabilish the connection to Java websocket server(using vert.x 3.2.1)?

Tags:

java

I'm trying to understand vert.x framework and to create webSocket server and client(using Java). In this tutorial I see small examples, but I don't understand how handler and websocket handler works.Also I don't understand how to organize session with message-changing between server and client. There is such example in tutorial for http-server, which uses websocket:

HttpServer server = Vertx.vertx().createHttpServer();
        server.websocketHandler(websocket -> {
            System.out.println("Connected!");
        }).listen(8080,"localhost");

Idea compiled id, but I don't see "Connected!" in terminal and don't know if it works. Are there any informative tutorials in internet about it?

like image 308
Iga Avatar asked Oct 18 '22 12:10

Iga


1 Answers

After many attempts I've realized web-socket connection between server and client using vert.x core 3.2.1. But I still have some questions. Server Code:

import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class serverTest {

    public static void main(String[] args) throws Exception {
HttpServer server = Vertx.vertx().createHttpServer();
        server.websocketHandler(new Handler<ServerWebSocket>() {
            @Override
            public void handle(ServerWebSocket webs) {
                System.out.println("Client connected");
                webs.writeBinaryMessage(Buffer.buffer("Hello user"));
                System.out.println("Client's message: ");
                webs.handler(data -> {System.out.println("Received data " + data.toString("ISO-8859-1"));});

            }
        });

        server.listen(8080, "localhost", res -> {
            if (res.succeeded()) {
                System.out.println("Server is now listening!");
            } else {
                System.out.println("Failed to bind!");
            }
        });
    }
}

Client's side:

import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClient;

public class clienTest {

    public static void main(String[] args) throws Exception{
 HttpClient client = Vertx.vertx().createHttpClient();

        client.websocket(8080, "localhost", "/some-uri", websocket ->
        {websocket.handler(data ->
        {
            System.out.println("Server message: ");
            System.out.println("Received data " + data.toString("ISO-8859-1"));});
            websocket.writeBinaryMessage(Buffer.buffer("Hello server"));
        });
   }
}

I have some questions, could anybody explain me some moments here? 1 webs.handler(data -> {System.out.println("Received data " + data.toString("ISO-8859-1"));- how can be written this code without using lambda-expression? 2 Server and client write onle 1 message to each other. How should I perform the session where the can write many messages to each other?(like in chat). I consider that it's necesssary to use a write-read thread. Does anybody know how it can be realized in vert.x?

like image 137
Iga Avatar answered Oct 21 '22 02:10

Iga