use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vert.x by eclipse.
the class WebSocketImplBase method writePartialMessage.
/**
* Splits the provided buffer into multiple frames (which do not exceed the maximum web socket frame size)
* and writes them in order to the socket.
*/
private void writePartialMessage(FrameType frameType, Buffer data, int offset) {
int end = offset + maxWebSocketFrameSize;
boolean isFinal;
if (end >= data.length()) {
end = data.length();
isFinal = true;
} else {
isFinal = false;
}
Buffer slice = data.slice(offset, end);
WebSocketFrame frame;
if (offset == 0 || !supportsContinuation) {
frame = new WebSocketFrameImpl(frameType, slice.getByteBuf(), isFinal);
} else {
frame = WebSocketFrame.continuationFrame(slice, isFinal);
}
writeFrame(frame);
int newOffset = offset + maxWebSocketFrameSize;
if (!isFinal) {
writePartialMessage(frameType, data, newOffset);
}
}
use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vert.x by eclipse.
the class WebSocketImplBase method writeBinaryFrameInternal.
private void writeBinaryFrameInternal(Buffer data) {
ByteBuf buf = data.getByteBuf();
WebSocketFrame frame = new WebSocketFrameImpl(FrameType.BINARY, buf);
writeFrame(frame);
}
use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vertx-web by vert-x3.
the class SockJSHandlerTest method testCombineTextFrameSockJs.
@Test
public void testCombineTextFrameSockJs() throws InterruptedException {
String serverPath = "/text-combine-sockjs";
setupSockJsServer(serverPath, this::echoRequest);
List<Buffer> receivedMessages = new ArrayList<>();
WebSocket openedWebSocket = setupSockJsClient(serverPath, receivedMessages);
Buffer largeMessage = Buffer.buffer("[\"" + TestUtils.randomAlphaString(30) + "\"]");
WebSocketFrame frame1 = new WebSocketFrameImpl(FrameType.TEXT, largeMessage.slice(0, 10).getByteBuf(), false);
WebSocketFrame frame2 = WebSocketFrame.continuationFrame(largeMessage.slice(10, 20), false);
WebSocketFrame frame3 = WebSocketFrame.continuationFrame(largeMessage.slice(20, largeMessage.length()), true);
log.debug("Client sending " + frame1.textData());
openedWebSocket.writeFrame(frame1);
log.debug("Client sending " + frame2.textData());
openedWebSocket.writeFrame(frame2);
log.debug("Client sending " + frame3.textData());
openedWebSocket.writeFrame(frame3);
await(5, TimeUnit.SECONDS);
assertEquals("Client should have received 2 messages: the reply and the close.", 2, receivedMessages.size());
Buffer expectedReply = Buffer.buffer("a" + largeMessage.toString());
assertEquals("Client reply should have matched request", expectedReply, receivedMessages.get(0));
assertEquals("Final message should have been a close", SOCKJS_CLOSE_REPLY, receivedMessages.get(1));
}
use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vert.x by eclipse.
the class WebSocketImplBase method writePartialMessage.
/**
* Splits the provided buffer into multiple frames (which do not exceed the maximum web socket frame size)
* and writes them in order to the socket.
*/
private Future<Void> writePartialMessage(WebSocketFrameType frameType, Buffer data, int offset) {
int end = offset + maxWebSocketFrameSize;
boolean isFinal;
if (end >= data.length()) {
end = data.length();
isFinal = true;
} else {
isFinal = false;
}
Buffer slice = data.slice(offset, end);
WebSocketFrame frame;
if (offset == 0 || !supportsContinuation) {
frame = new WebSocketFrameImpl(frameType, slice.getByteBuf(), isFinal);
} else {
frame = WebSocketFrame.continuationFrame(slice, isFinal);
}
int newOffset = offset + maxWebSocketFrameSize;
if (isFinal) {
return writeFrame(frame);
} else {
writeFrame(frame);
return writePartialMessage(frameType, data, newOffset);
}
}
use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vert.x by eclipse.
the class WebSocketTest method testServerWebSocketReceivePingExceedsMaxFrameSize.
@Test
public void testServerWebSocketReceivePingExceedsMaxFrameSize() {
String pingBody = randomAlphaString(113);
Integer maxFrameSize = 64;
Buffer ping1 = Buffer.buffer(Buffer.buffer(pingBody.getBytes()).getBytes(0, maxFrameSize));
Buffer ping2 = Buffer.buffer(Buffer.buffer(pingBody.getBytes()).getBytes(maxFrameSize, pingBody.length()));
server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebSocketFrameSize(maxFrameSize));
server.webSocketHandler(ws -> {
}).listen(onSuccess(v -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(ws -> {
List<Buffer> pongs = new ArrayList<>();
ws.pongHandler(pong -> {
pongs.add(pong);
if (pongs.size() == 2) {
assertEquals(pongs, Arrays.asList(ping1, ping2));
testComplete();
}
});
try {
ws.writeFrame(new WebSocketFrameImpl(WebSocketFrameType.PING, ping1.copy().getByteBuf(), false));
ws.writeFrame(new WebSocketFrameImpl(WebSocketFrameType.PING, ping2.copy().getByteBuf(), true));
} catch (Throwable t) {
fail(t);
}
}));
}));
await();
}
Aggregations