2

我在 java vertx 中有一个工作服务器和一个客户端,但是在 SockJS 中,我想在 java 中制作这个客户端,我怎样才能将它转换为 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)
}

我正在寻找的主要是vertx客户端连接到我的vertx服务器的基本启动代码

4

1 回答 1

1

试试这个例子

发送和接收消息的主要代码:

    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"));
    });
于 2019-11-21T15:08:00.103 回答