java 可以通过 WebSocket 传递的最大数据大小是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32309727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the maximum size of data that can be passed through a WebSocket?
提问by theapache64
I am new in Java WebSocket
. When I tried to pass a JSON
with length greater than 8192
, the websocket disconnected immediately. How ever JSON
with length <= 8191
works fine.
我是 Java 新手WebSocket
。当我尝试传递JSON
长度大于 的 时8192
,websocket 立即断开连接。JSON
长度如何<= 8191
工作正常。
Is there any MAX SIZE/ LIMIT
of data that can be passed through a WebSocket
? if yes, what's that size?
是否有任何MAX SIZE/ LIMIT
数据可以通过 a 传递WebSocket
?如果是,那尺寸是多少?
I FIXED THE ERROR BY ADDING THESE LINES TO my web.xml
我通过将这些行添加到我的 web.xml
<context-param>
<param-name>org.apache.tomcat.websocket.textBufferSize</param-name>
<param-value>32768</param-value>
</context-param>
<context-param>
<param-name>org.apache.tomcat.websocket.binaryBufferSize</param-name>
<param-value>32768</param-value>
</context-param>
Thanks @Davide Lorenzo MARINO.
谢谢@Davide Lorenzo MARINO。
回答by Davide Lorenzo MARINO
IT is actually a value very big and probably you don't worry about it.
它实际上是一个非常大的价值,您可能并不担心。
A single frame, by RFC-6455 base framing, has a maximum size limit of 18,446,744,073,709,551,615 bytes (maximum value of a 64-bit unsigned value).
根据 RFC-6455 基本帧,单个帧的最大大小限制为 18,446,744,073,709,551,615 字节(64 位无符号值的最大值)。
Try only to make it as little as possible to handle your requirements.
尽量让它尽可能少地处理您的要求。
Because the problem is generated on the server side (tomcat). Checking the tomcat documentation I see that:
因为问题是在服务器端(tomcat)产生的。检查 tomcat 文档我看到:
The default buffer size for binary messages is 8192 bytes. This may be changed for a web application by setting the servlet context initialization parameter org.apache.tomcat.websocket.binaryBufferSize to the desired value in bytes.
二进制消息的默认缓冲区大小为8192 字节。对于 Web 应用程序,这可以通过将 servlet 上下文初始化参数 org.apache.tomcat.websocket.binaryBufferSize 设置为所需的字节值来更改。
So you can change it using updating the org.apache.tomcat.websocket.binaryBufferSizeparameter in the configuration file of tomcat.
因此,您可以通过更新tomcat 配置文件中的org.apache.tomcat.websocket.binaryBufferSize参数来更改它。
For additional informations please see the tomcat guide here
有关其他信息,请参阅此处的 tomcat 指南
回答by Javi
About the question related with WebSocket getting disconnected, I had same problem in my Android client that was receiving next message "Binary message size exceeds maximum size".
关于与 WebSocket 断开连接相关的问题,我在接收下一条消息“二进制消息大小超过最大大小”的 Android 客户端中遇到了同样的问题。
After looking for some solution in all the Jetty documentation and rest of web pages, the only working option that I have got is to include next code "maxBinaryMessageSize = size * 1024" (where size is your needed maximum limit from client point of view) in the WebSocket class definition, as you can see in next lines:
在所有 Jetty 文档和其余网页中寻找一些解决方案后,我唯一的工作选择是包含下一个代码“maxBinaryMessageSize = size * 1024”(其中 size 是您从客户端角度所需的最大限制)在 WebSocket 类定义中,您可以在下一行中看到:
import org.eclipse.jetty.websocket.api.BatchMode;
import org.eclipse.jetty.websocket.api.RemoteEndpoint;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
@WebSocket(maxBinaryMessageSize = 1024 * 1024)
public class MyClientWebSocket
{
@OnWebSocketClose
public void onClose(int statusCode, String reason)
{
closeLatch.countDown();
}
@OnWebSocketError
public void onError(Throwable reason)
{
//TODO ACORDARSE DE QUITAR ESTO
Log.w(TAG, "+++ ERROR : " + reason);
}
@OnWebSocketConnect
public void onConnect(Session session)
{
// your code
}
@OnWebSocketMessage
public void onMessageBinary(byte[] message, int offset, int length)
{
// message can be processed with a maximum of 1024 * 1024 bytes and not anymore limited to 64 * 1024
}
}
If there is some WebSocket expert that can confirm if this is a correct solution from client side (in Java/Android), this would be great. Thanks.
如果有一些 WebSocket 专家可以从客户端(在 Java/Android 中)确认这是否是正确的解决方案,那就太好了。谢谢。