Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-dev] Programmatic WebSocket upgrade on Jetty 10


Is there any reason you can't extend the JettyWebSocketServlet instead of just HttpServlet? This way you could use only the Jetty WebSocket API and avoid these websocket-core and internal classes you would need to use otherwise. You can use the JettyWebSocketCreator to do any logic to select which WebSocket Endpoint you will use, and you can map the creator to "/*" so it matches everything.

public static class CustomUpgradeServlet extends JettyWebSocketServlet
    protected void configure(JettyWebSocketServletFactory factory)
        factory.addMapping("/*", (req, resp) ->
            if (condition1)
                return new MyEchoSocket1();
            if (condition2)
                return new MyEchoSocket2();
            if (condition3)
                return new MyEchoSocket3();


On Fri, Jan 8, 2021 at 9:24 AM Rossen Stoyanchev <rstoyanchev@xxxxxxxxxx> wrote:

I need to support WebSocket upgrades on Jetty 10 from within a web framework. Like what JettyWebSocketServlet or WebSocketUpgradeFilter do internally but not actually using them nor relying on their mapping mechanism.

My initial attempt at a minimal custom Servlet to do this:

public class MyServlet extends HttpServlet {

    private final Handshaker handshaker = new HandshakerSelector();

    private WebSocketComponents components;

    private JettyServerFrameHandlerFactory frameHandlerFactory;

    public void init(ServletConfig config) throws ServletException {
        this.components = WebSocketServerComponents.getWebSocketComponents(getServletContext());
        this.frameHandlerFactory = JettyServerFrameHandlerFactory.getFactory(getServletContext());

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Configuration.Customizer customizer = new Configuration.ConfigurationCustomizer();
        WebSocketCreator creator = (req1, resp1) -> new MyEchoSocket();
        CreatorNegotiator negotiator = new CreatorNegotiator(creator, this.frameHandlerFactory, customizer);
        this.handshaker.upgradeRequest(negotiator, req, resp, this.components, null);
However, several types are from "internal" packages. Is there a better to do this? It would be very useful to have some basic API for programmatic WebSocket upgrades from a framework or application Servlet.

jetty-dev mailing list
To unsubscribe from this list, visit

Back to the top