@Test public void testSendMessage() { var wsMockHelper = new WebSocketMockHelper(); getTestByteArrayStream().forEach(bytes -> { try { WsProducer.sendMessage(wsMockHelper.getMock(), bytes, false); assertArrayEquals("sendMessage() (bytes, non-streaming) seems flawed", bytes, wsMockHelper.getResultBytes()); wsMockHelper.resetBuffer(); WsProducer.sendMessage(wsMockHelper.getMock(), bytes, true); assertArrayEquals("sendMessage() (bytes, streaming) seems flawed", bytes, wsMockHelper.getResultBytes()); wsMockHelper.resetBuffer(); var string = new String(bytes, StandardCharsets.US_ASCII); WsProducer.sendMessage(wsMockHelper.getMock(), string, false); assertArrayEquals("sendMessage() (String, non-streaming) seems flawed", bytes, wsMockHelper.getResultBytes()); wsMockHelper.resetBuffer(); WsProducer.sendMessage(wsMockHelper.getMock(), string, true); assertArrayEquals("sendMessage() (String, streaming) seems flawed", bytes, wsMockHelper.getResultBytes()); wsMockHelper.resetBuffer(); } catch (Exception e) { throw new RuntimeException(e); } }); }
public static void sendMessage(WebSocket webSocket, String msg, boolean streaming) { if (streaming && msg.length() > STREAM_BUFFER_SIZE) { int p = 0; while (p < msg.length()) { if (msg.length() - p < STREAM_BUFFER_SIZE) { webSocket.sendContinuationFrame(msg.substring(p), true, 0); p = msg.length(); } else if (p == 0) { p = STREAM_BUFFER_SIZE; webSocket.sendTextFrame(msg.substring(0, STREAM_BUFFER_SIZE), false, 0); } else { var substring = msg.substring(p, p + STREAM_BUFFER_SIZE); p += STREAM_BUFFER_SIZE; webSocket.sendContinuationFrame(substring, msg.length() == p, 0); } } } else { webSocket.sendTextFrame(msg); } }
WsProducer extends DefaultProducer { public static void sendMessage(WebSocket webSocket, String msg, boolean streaming) { if (streaming && msg.length() > STREAM_BUFFER_SIZE) { int p = 0; while (p < msg.length()) { if (msg.length() - p < STREAM_BUFFER_SIZE) { webSocket.sendContinuationFrame(msg.substring(p), true, 0); p = msg.length(); } else if (p == 0) { p = STREAM_BUFFER_SIZE; webSocket.sendTextFrame(msg.substring(0, STREAM_BUFFER_SIZE), false, 0); } else { var substring = msg.substring(p, p + STREAM_BUFFER_SIZE); p += STREAM_BUFFER_SIZE; webSocket.sendContinuationFrame(substring, msg.length() == p, 0); } } } else { webSocket.sendTextFrame(msg); } } }
WsProducer extends DefaultProducer { public static void sendMessage(WebSocket webSocket, String msg, boolean streaming) { if (streaming && msg.length() > STREAM_BUFFER_SIZE) { int p = 0; while (p < msg.length()) { if (msg.length() - p < STREAM_BUFFER_SIZE) { webSocket.sendContinuationFrame(msg.substring(p), true, 0); p = msg.length(); } else if (p == 0) { p = STREAM_BUFFER_SIZE; webSocket.sendTextFrame(msg.substring(0, STREAM_BUFFER_SIZE), false, 0); } else { var substring = msg.substring(p, p + STREAM_BUFFER_SIZE); p += STREAM_BUFFER_SIZE; webSocket.sendContinuationFrame(substring, msg.length() == p, 0); } } } else { webSocket.sendTextFrame(msg); } } WsProducer(WsEndpoint endpoint); }
WsProducer extends DefaultProducer { public static void sendMessage(WebSocket webSocket, String msg, boolean streaming) { if (streaming && msg.length() > STREAM_BUFFER_SIZE) { int p = 0; while (p < msg.length()) { if (msg.length() - p < STREAM_BUFFER_SIZE) { webSocket.sendContinuationFrame(msg.substring(p), true, 0); p = msg.length(); } else if (p == 0) { p = STREAM_BUFFER_SIZE; webSocket.sendTextFrame(msg.substring(0, STREAM_BUFFER_SIZE), false, 0); } else { var substring = msg.substring(p, p + STREAM_BUFFER_SIZE); p += STREAM_BUFFER_SIZE; webSocket.sendContinuationFrame(substring, msg.length() == p, 0); } } } else { webSocket.sendTextFrame(msg); } } WsProducer(WsEndpoint endpoint); @Override WsEndpoint getEndpoint(); @Override void process(Exchange exchange); static void sendMessage(WebSocket webSocket, String msg, boolean streaming); static void sendMessage(WebSocket webSocket, byte[] msg, boolean streaming); static void sendStreamMessage(WebSocket webSocket, InputStream in); }
WsProducer extends DefaultProducer { public static void sendMessage(WebSocket webSocket, String msg, boolean streaming) { if (streaming && msg.length() > STREAM_BUFFER_SIZE) { int p = 0; while (p < msg.length()) { if (msg.length() - p < STREAM_BUFFER_SIZE) { webSocket.sendContinuationFrame(msg.substring(p), true, 0); p = msg.length(); } else if (p == 0) { p = STREAM_BUFFER_SIZE; webSocket.sendTextFrame(msg.substring(0, STREAM_BUFFER_SIZE), false, 0); } else { var substring = msg.substring(p, p + STREAM_BUFFER_SIZE); p += STREAM_BUFFER_SIZE; webSocket.sendContinuationFrame(substring, msg.length() == p, 0); } } } else { webSocket.sendTextFrame(msg); } } WsProducer(WsEndpoint endpoint); @Override WsEndpoint getEndpoint(); @Override void process(Exchange exchange); static void sendMessage(WebSocket webSocket, String msg, boolean streaming); static void sendMessage(WebSocket webSocket, byte[] msg, boolean streaming); static void sendStreamMessage(WebSocket webSocket, InputStream in); static final int STREAM_BUFFER_SIZE; }