I have a little application that has a @ServerEndpoint defined (for websockets). Now i would like to inject a business object that does some logic, so i can work with that.
Here i create the jetty server and tell him to use my EventSocket.class which contains the @ServerEndpoint("/").
public JettyServer(Application application) {
this.application = application;
this.server = new Server(8080);
EventSocket.application = application; //so nasty, need to figure out how to inject correctly in constructor
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
server.setHandler(servletContextHandler);
// Add jakarta.websocket support
JakartaWebSocketServletContainerInitializer.configure(servletContextHandler, (context, container) ->
{
container.addEndpoint(EventSocket.class);
});
// Add default servlet (to serve the html/css/js)
// Figure out where the static files are stored.
URL urlStatics = Thread.currentThread().getContextClassLoader().getResource("index.html");
Objects.requireNonNull(urlStatics, "Unable to find index.html in classpath");
String urlBase = urlStatics.toExternalForm().replaceFirst("/[^/]*$", "/");
ServletHolder defHolder = new ServletHolder("default", new DefaultServlet());
defHolder.setInitParameter("resourceBase", urlBase);
defHolder.setInitParameter("dirAllowed", "true");
servletContextHandler.addServlet(defHolder, "/");
}
Each time a request comes in, jetty creates a new instance of this EventSocket object. Now i need to somehow modify jetty that it uses a special configuration when it wants to create that object, because i need to define a listener in this EventSocket.java.
The part where i need this listener in EventSocket:
@OnMessage
public void onMessage(String message) {
System.out.println(message);
String command = message.split(":")[0];
switch (command) {
case "x": listener.gotCalledWith(message); break;
}
listener is not defined, it should be passed to the default constructor of EventSocket, but thats not possible because i have no clue how to configure jetty to use another constructor. I already created https://github.com/jetty/jetty.project/issues/11502 but no answer yet. Any hint/idea is appreciated!