Switched from text to binary messages.

master
Tomasz Polgrabia 2024-12-27 19:39:27 +01:00
parent b29a3128bd
commit baf413a063
3 changed files with 11 additions and 9 deletions

View File

@ -56,7 +56,7 @@ public class ClientPeriodicStdinScanner implements Handler<Long> {
Buffer b = Buffer.buffer(len); Buffer b = Buffer.buffer(len);
b = b.appendBytes(clientPayload, prevIdx, len); b = b.appendBytes(clientPayload, prevIdx, len);
b = b.appendByte((byte)'\n'); b = b.appendByte((byte)'\n');
webSocket.writeTextMessage(b.toString(StandardCharsets.UTF_8)); webSocket.writeBinaryMessage(b);
prevIdx = idx + eolLength; prevIdx = idx + eolLength;
} }
} while (idx >= 0 && prevIdx < clientPayload.length); } while (idx >= 0 && prevIdx < clientPayload.length);

View File

@ -56,7 +56,7 @@ public class WebsocketClientParticle extends AbstractVerticle {
)); ));
webSocket webSocket
.textMessageHandler(msg -> logger.info("Got message: {}", msg)) .binaryMessageHandler(msg -> logger.info("Got message: {}", msg))
.closeHandler(unused -> { .closeHandler(unused -> {
try { try {
stop(); stop();

View File

@ -2,31 +2,33 @@ package ch.polgrabia.demos.server;
import ch.polgrabia.demos.client.WebsocketClientApp; import ch.polgrabia.demos.client.WebsocketClientApp;
import io.vertx.core.Handler; import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.ServerWebSocket; import io.vertx.core.http.ServerWebSocket;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class WebsocketServerHandler implements Handler<ServerWebSocket> { public class WebsocketServerHandler implements Handler<ServerWebSocket> {
private static final Logger logger = LoggerFactory.getLogger(WebsocketClientApp.class); private static final Logger logger = LoggerFactory.getLogger(WebsocketServerHandler.class);
@Override @Override
public void handle(ServerWebSocket serverWebSocket) { public void handle(ServerWebSocket serverWebSocket) {
serverWebSocket.writeTextMessage("Hello"); serverWebSocket.writeBinaryMessage(Buffer.buffer("Hello"));
serverWebSocket.textMessageHandler(new WebsocketTextMessageHandler(serverWebSocket)); serverWebSocket.handler(new WebsocketMessageHandler(serverWebSocket));
} }
static class WebsocketTextMessageHandler implements Handler<String> { static class WebsocketMessageHandler implements Handler<Buffer> {
private final ServerWebSocket serverWebSocket; private final ServerWebSocket serverWebSocket;
public WebsocketTextMessageHandler(ServerWebSocket serverWebSocket) { public WebsocketMessageHandler(ServerWebSocket serverWebSocket) {
this.serverWebSocket = serverWebSocket; this.serverWebSocket = serverWebSocket;
this.serverWebSocket.setWriteQueueMaxSize(0);
} }
@Override @Override
public void handle(String s) { public void handle(Buffer s) {
logger.info("Got message {}", s); logger.info("Got message {}", s);
serverWebSocket.writeTextMessage("Pong: " + s); // TBD send to kafka serverWebSocket.writeBinaryMessage(Buffer.buffer("Pong: " + s.toString() + "\n")); // TBD send to kafka
} }
} }