ZedPython
ZedPython

Reputation: 121

Replacing JavaScript SockJS client with Java Vert.x client

I have a working server in java vertx and a client but in SockJS, i would like to make this client in java, how can i convert this to java:

const EB_EVENT_TO_SERVER = "event.to.server";
const EB_EVENT_TO_CLIENT = "event.to.client.";
const EB_EVENT_TO_CLIENTS = "event.to.clients";

let EB_PATH;
let eb;

function initEventBus()
{
	EB_PATH  = `${config.host}/${config.folder}/${config.group}/events`;
	eb =  new EventBus(EB_PATH);
	eb.onopen = openEventBus;
}

function sendEventToServer(message)
{
	eb.send(EB_EVENT_TO_SERVER, message);
}

function openEventBus()
{
	eb.registerHandler(EB_EVENT_TO_CLIENT + playerId, (error, message) => onReceiveUnicast(error, message));
	eb.registerHandler(EB_EVENT_TO_CLIENTS, (error, message) => onReceiveBroadcast(error, message));
}

function translateUnicast(json)
{
    if (json.hasOwnProperty("players") && json.hasOwnProperty("bricks"))
        translateJsonToCanvas(json);
}

function translateBroadcast(json)
{

}

function onReceiveUnicast(error, message)
{
    if (error)
        console.error("Something went wrong on the EventBus while receiving Unicast");
    else
        translateUnicast(message.body)
}

function onReceiveBroadcast(error, message)
{
    if (error)
        console.error("Something went wrong on the EventBus while receiving Broadcast");
    else
        translateBroadcast(message.body)
}

What i'm looking for mainly is the basic start code for vertx client to connect to my vertx server

Upvotes: 2

Views: 346

Answers (1)

lczapski
lczapski

Reputation: 4120

Try this example

The main code to send and receive messages:

    HttpClient client = vertx.createHttpClient();

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

Upvotes: 1

Related Questions