如何在 Java 中为 Web 套接字客户端 (Jetty) 设置最大缓冲区大小

How can I set max buffer size for web socket client(Jetty) in Java

有一个连接web socket连接的函数。

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);
}

你可以找到 'setMaxBinaryMessageBufferSize' 以便我将最大缓冲区大小设置为 700000。

但是当onMessageBuffer 事件发生时,我可以找到如下异常。

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.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)

如何设置 Web 套接字连接的最大缓冲区大小以防止出现异常?

有2个setMaxTextMessageBufferSize,setMaxBinaryMessageBufferSize

取决于您发送的数据类型:

    wsClient = new WebSocketClient(factory);
    wsClient.setMaxTextMessageBufferSize(Args.maxTextSize);
    wsClient.setMaxBinaryMessageBufferSize(Args.maxTextSize);

https://www.eclipse.org/jetty/javadoc/jetty-11/org/eclipse/jetty/websocket/client/WebSocketClient.html

您还可以选择在 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);