I ran into this issue and had some difficulty finding answers for this anywhere so I thought I would enter it here for future programmers.
In Jetty 9, if you try to set the maximum message size on a session object to handle large data packets, it will not work. You will still get disconnected if your client tries to send large data. I'm talking about setMaximimumMessageSize on this object: http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/Session.html
Instead, what you have to do is set the max message size on the policy object acquired from the WebSocketServletFactory.
public final class MyWebSocketServlet extends WebSocketServlet
{
private static final long MAX_MESSAGE_SIZE = 1000000;
@Override
public void configure(WebSocketServletFactory factory)
{
factory.getPolicy().setMaxMessageSize(MAX_MESSAGE_SIZE);
factory.setCreator(new MyWebSocketCreator());
}
}
This will work as intended and your server will be able to handle large messages up to the maximum size you set.
The way you are setting the maximum message, in the WebSocketServlet
is correct.
The Session.setMaximumMessageSize(long) as you pointed out in the javadoc is an unfortunately leaking of an early draft of JSR-356 (javax.websocket API) effort.
That method on the Jetty side API should not be there, and has been removed in Jetty 9.1
Bug has been filed: https://bugs.eclipse.org/bugs/show_bug.cgi?id=412439
Note: Jetty 9.1 will have the JSR-356 (javax.websocket API) support in it. Where the javax.websocket.Session has 2 methods of similar behavior.
javax.websocket.Session.setMaxBinaryMessageBufferSize(int)
javax.websocket.Session.setMaxTextMessageBufferSize(int)
I had this problem when sending files (binary data) with more than 64KB. I was using the javax.websocket-example from the Embedded Jetty WebSocket Examples.
Finally the only thing I need to do was to setMaxBinaryMessageBufferSize
in the Session
argument from the @OnOpen
annotated method.
@ClientEndpoint
@ServerEndpoint(value = "/ws")
public class EventSocket {
@OnOpen
public void onWebSocketConnect(Session sess) {
sess.setMaxBinaryMessageBufferSize(1 * 1024 * 1024); // 1MB
}
@OnMessage
public void processUpload(byte[] b, boolean last, Session session) {
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With