There is a function for connect web socket connection.
public void connectWebSocket(String vaHost, String apiKey) throws Exception{
logger.info("========= CONNECT WEBSOCKET =========");
String destUri = "ws://" vaHost "/metadata";
WebSocketClient client = new WebSocketClient();
SimpleEchoSocket socket = new SimpleEchoSocket();
client.start();
client.setConnectTimeout(-1);
URI echoUri = new URI(destUri);
ClientUpgradeRequest requestws = new ClientUpgradeRequest();
requestws.setSubProtocols("va-metadata");
requestws.setTimeout(-1, TimeUnit.SECONDS);
client.setMaxBinaryMessageBufferSize(700000);
client.connect(socket,echoUri,requestws);
logger.info("=========WS Connecting to :" echoUri);
}
You can find 'setMaxBinaryMessageBufferSize' so that I set max buffer size to 700000.
But when an onMessageBuffer event occurs, I can find the exception like below.
org.eclipse.jetty.websocket.api.MessageTooLargeException: Binary message size [484666] exceeds maximum size [65536]
at org.eclipse.jetty.websocket.api.WebSocketPolicy.assertValidBinaryMessageSize(WebSocketPolicy.java:128)
at org.eclipse.jetty.websocket.common.Parser.assertSanePayloadLength(Parser.java:133)
at org.eclipse.jetty.websocket.common.Parser.parseFrame(Parser.java:494)
at org.eclipse.jetty.websocket.common.Parser.parseSingleFrame(Parser.java:253)
at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onFillable(AbstractWebSocketConnection.java:459)
at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onFillable(AbstractWebSocketConnection.java:440)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105)
at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:336)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:313)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:171)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:129)
at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:383)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:882)
at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1036)
at java.base/java.lang.Thread.run(Thread.java:831)
How can I set max buffer size for web socket connection to prevent exception?
CodePudding user response:
There are 2 setMaxTextMessageBufferSize
,setMaxBinaryMessageBufferSize
Depending on the type of data you are sending:
wsClient = new WebSocketClient(factory);
wsClient.setMaxTextMessageBufferSize(Args.maxTextSize);
wsClient.setMaxBinaryMessageBufferSize(Args.maxTextSize);
You also have the option of setting it on the Session
:
CompletableFuture<Session> futureSession = wsClient.connect(webSocket, uri);
Session session = futureSession.get();
session.getPolicy().setMaxBinaryMessageBufferSize(Args.maxTextSize);
session.getPolicy().setMaxBinaryMessageSize(Args.maxTextSize);
session.getPolicy().setMaxTextMessageBufferSize(Args.maxTextSize);
session.getPolicy().setMaxTextMessageSize(Args.maxTextSize);