Hi,
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:
@SuppressWarnings("serial")
public class MyServlet extends HttpServlet {
private final Handshaker handshaker = new HandshakerSelector();
private WebSocketComponents components;
private JettyServerFrameHandlerFactory frameHandlerFactory;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.components = WebSocketServerComponents.getWebSocketComponents(getServletContext());
this.frameHandlerFactory = JettyServerFrameHandlerFactory.getFactory(getServletContext());
}
@Override
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.
Thanks,
Rossen