Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetty 9 WebSocket Server Max Message Size on Session

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.

like image 638
Sanjeev Avatar asked Jul 05 '13 21:07

Sanjeev


2 Answers

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)
like image 185
Joakim Erdfelt Avatar answered Nov 08 '22 21:11

Joakim Erdfelt


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

}
like image 2
lmiguelmh Avatar answered Nov 08 '22 21:11

lmiguelmh