简体   繁体   English

Java WebSocket 消息限制

[英]Java WebSocket message limit

I'm trying to create communication between simple Java App (using java.net.http.WebSocket class) and remote google-chrome run using google-chrome --remote-debugging-port=9222 --user-data-dir=. I'm trying to create communication between simple Java App (using java.net.http.WebSocket class) and remote google-chrome run using google-chrome --remote-debugging-port=9222 --user-data-dir=.

Sending and receiving small messages works as expected, but there is an issue in case of bigger messages, 16kb.发送和接收小消息按预期工作,但对于较大的消息(16kb)存在问题。

Here is part of java source:这是 java 源代码的一部分:


var uri = new URI("ws://127.0.0.1:9222/devtools/page/C0D7B4DBC53FB39F7A4BE51DA79E96BB");

/// create websocket client
WebSocket ws = HttpClient
    .newHttpClient()
    .newWebSocketBuilder()
    .connectTimeout(Duration.ofSeconds(30))
    .buildAsync(uri, simpleListener)
    .join();

// session Id attached to chrome tab
String sessionId = "...";

// send message
String message = "{\"id\":1,\"method\":\"Runtime.evaluate\",\"params\":{\"expression\":\"document.body.style.backgroundColor = 'blue';\",\"returnByValue\":true,\"awaitPromise\":true,\"userGesture\":true},\"sessionId\":\"" + sessionId + "\"}";

// this works
ws.send(message, true);

// generate big string contains over 18k chars for testing purpose
String bigMessage = "{\"id\":2,\"method\":\"Runtime.evaluate\",\"params\":{\"expression\":\"[" + ("1,".repeat(9000)) + "1]\",\"returnByValue\":true,\"awaitPromise\":true,\"userGesture\":true},\"sessionId\":\"" + sessionId + "\"}";

// this doesn't work
ws.send(bigMessage, true);

Here is stack:这是堆栈:

java.net.SocketException: Connection reset
    at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:345)
    at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:376)
    at java.net.http/jdk.internal.net.http.SocketTube.readAvailable(SocketTube.java:1153)
    at java.net.http/jdk.internal.net.http.SocketTube$InternalReadPublisher$InternalReadSubscription.read(SocketTube.java:821)
    at java.net.http/jdk.internal.net.http.SocketTube$SocketFlowTask.run(SocketTube.java:175)
    at java.net.http/jdk.internal.net.http.common.SequentialScheduler$SchedulableTask.run(SequentialScheduler.java:198)
...

I've tried basically the same by using puppeteer (nodejs library) and it works as expected.我通过使用puppeteer (nodejs 库)尝试了基本相同的方法,它按预期工作。

I can't find any resource online about this issue.我在网上找不到有关此问题的任何资源。 Is there anything I'm missing in my example?我的例子中有什么我遗漏的吗?


Here is url to simple example: https://github.com/zeljic/websocket-devtools-protocol这是 url 的简单示例: https://github.com/zeljic/websocket-devtools-protocol

Based on what I've seen so far, my best guess would be that Chrome Dev Tools do not process fragmented Text messages on that exposed webSocketDebuggerUrl endpoint.根据我目前所见,我最好的猜测是 Chrome 开发工具不会在暴露webSocketDebuggerUrl端点上处理碎片化的文本消息。 Whether Chrome Dev Tools can be configured to do so or not, is another question.是否可以将 Chrome 开发工具配置为这样做是另一个问题。 I must note, however, that RFC 6455 (The WebSocket Protocol) mandates it:但是,我必须指出,RFC 6455(WebSocket 协议)要求它:

Clients and servers MUST support receiving both fragmented and unfragmented messages.客户端和服务器必须支持接收分片和非分片消息。

There's one workaround I can see here.我可以在这里看到一种解决方法。 Keep in mind that this is unsupported and may change in the future unexpectedly.请记住,这是不受支持的,并且将来可能会意外更改。 When running your client, specify the following system property on the command line -Djdk.httpclient.websocket.intermediateBufferSize=1048576 (or pick any other suitable size).运行客户端时,在命令行上指定以下系统属性-Djdk.httpclient.websocket.intermediateBufferSize=1048576 (或选择任何其他合适的大小)。 As long as you keep sending your messages with true passed as boolean last argument to the send* methods, java.net.http.WebSocket will send messages unfragment, in a single WebSocket frame. As long as you keep sending your messages with true passed as boolean last argument to the send* methods, java.net.http.WebSocket will send messages unfragment, in a single WebSocket frame.

Well I had a similar issue when sending a big string by using web-sockets in java with a tomcat server.好吧,我在使用 java 和 tomcat 服务器发送大字符串时遇到了类似的问题。 There can be payload limit to send or receive in websocket server.在 websocket 服务器中发送或接收的负载可能存在限制。


checkout org.apache.tomcat.websocket.textBufferSize in tomcat's doc .结帐 org.apache.tomcat.websocket.textBufferSize 在tomcat 的文档中。 By default it is 8192 bytes try increasing the size.默认情况下它是8192字节尝试增加大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM