From 3702211b5ae8d15e4137d73aa725ecf896a870de Mon Sep 17 00:00:00 2001 From: zb-sr <155634845+zb-sr@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:11:55 +0200 Subject: [PATCH] better ws support for large messages --- .../sdk/internal/connection/WebSocketConnection.java | 11 ++--------- .../mbs/sdk/internal/protocol/ProtocolEngine.java | 2 +- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/sdk/src/main/java/com/sportradar/mbs/sdk/internal/connection/WebSocketConnection.java b/src/sdk/src/main/java/com/sportradar/mbs/sdk/internal/connection/WebSocketConnection.java index 3d78578c..21da2cb0 100644 --- a/src/sdk/src/main/java/com/sportradar/mbs/sdk/internal/connection/WebSocketConnection.java +++ b/src/sdk/src/main/java/com/sportradar/mbs/sdk/internal/connection/WebSocketConnection.java @@ -8,13 +8,11 @@ import com.sportradar.mbs.sdk.internal.connection.msg.base.WsInputMessage; import com.sportradar.mbs.sdk.internal.connection.msg.base.WsOutputMessage; import org.java_websocket.client.WebSocketClient; -import org.java_websocket.framing.Framedata; -import org.java_websocket.framing.TextFrame; +import org.java_websocket.enums.Opcode; import org.java_websocket.handshake.ServerHandshake; import java.net.URI; import java.nio.ByteBuffer; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -102,14 +100,9 @@ private void sendLoop() { } private void sendMsg(final WebSocket ws, final List msgs) { - final List frames = new ArrayList<>(); for (int i = 0; i < msgs.size(); i++) { - final TextFrame frame = new TextFrame(); - frame.setPayload(msgs.get(i)); - frame.setFin(i == msgs.size() - 1); - frames.add(frame); + ws.sendFragmentedFrame(Opcode.TEXT, msgs.get(i), i == (msgs.size() - 1)); } - ws.sendFrame(frames); } private void reconnectWebSocket(final WebSocket ws, final boolean throwExc) { diff --git a/src/sdk/src/main/java/com/sportradar/mbs/sdk/internal/protocol/ProtocolEngine.java b/src/sdk/src/main/java/com/sportradar/mbs/sdk/internal/protocol/ProtocolEngine.java index 09434496..568ad0a5 100644 --- a/src/sdk/src/main/java/com/sportradar/mbs/sdk/internal/protocol/ProtocolEngine.java +++ b/src/sdk/src/main/java/com/sportradar/mbs/sdk/internal/protocol/ProtocolEngine.java @@ -147,7 +147,7 @@ private List createFrames(final Request request) throws JsonProcessi int offset = 0; while (offset < bytes.length) { final int chunkSize = Math.min(bytes.length - offset, MAX_CHUNK_SIZE); - final ByteBuffer buffer = ByteBuffer.wrap(bytes, offset, offset + chunkSize); + final ByteBuffer buffer = ByteBuffer.wrap(bytes, offset, chunkSize); result.add(buffer); offset += chunkSize; }