Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Jetty 9 and WebSocket

Hi,

I have a very basic question.

I managed to write a HttpServer with Jetty8. Now I am trying to write a WebSocket with Jetty 9. I followed the tutorial for creating a WebSocket-Server using WebSocketServlet and WebSocketAdapter. But when I do the echo-Test with websocket.org/echo.html, I don't get the response,only CONNECTED and SENT: message.  What do I need add to my classes, so that it works?

Thanks for your help

Alex

My classes are:

public class MyWebSocketServlet extends WebSocketServlet {
    public void configure(WebSocketServletFactory factory) {
         factory.register(MyWebSocket.class);
    }
}

public class MyWebSocket  extends WebSocketAdapter{
    public void onWebSocketText(String message) {
        if (isNotConnected()) {
           return; }
        try{
            this.session.getRemote().sendString("You sent: "+message);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
   
}

public class MyServer {
  public static void main(String[] args) throws Exception {  
      Server server = new Server(8050);
      ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
      server.setHandler(context);
      context.addServlet(new ServletHolder(new MyWebSocketServlet()),"/*");

      server.start();
      server.join();
  }
}
 



Back to the top