Websocket JSR-356 fail with Jetty 9.4.1

前端 未结 1 677
遥遥无期
遥遥无期 2020-12-11 14:24

My current web server is embedded Jetty 9.1.5. It works well with JSR-356 to create websocket. These days, I am trying to upgrade to Jetty 9.4.1. Everything works nicely exc

相关标签:
1条回答
  • 2020-12-11 15:07

    Judging from your setup, you'll wind up with ...

    wss://localhost:8443/myContext/ws/communication/5/kbui/None

    Your contextPath isn't /context, its actually /myContext in your setup.

    You trimmed out the Servlet Mappings section on the dump (that was the important part. heh)

    But attempting to manually add the WSCommunication Endpoint contained in WebAppContext from outside of the WebAppClassloader or the WebApp's ServletContext is probably going to be a problem as well.

    You have 3 options:

    1. The JSR356 Automatic Way

      Setup bytecode scanning and annotation discovery for your WebAppContext and let the startup discover and auto-load the WSCommunication endpoint.

      Add the following to your webContext ...

    webContext.setAttribute("org.eclipse.jetty.websocket.jsr356",Boolean.TRUE);
    
    webContext.setConfigurations(new Configuration[] {
                new AnnotationConfiguration(),
                new WebXmlConfiguration(),
                new WebInfConfiguration(),
                new PlusConfiguration(), 
                new MetaInfConfiguration(),
                new FragmentConfiguration(), 
                new EnvConfiguration()});
    

    And add the jetty-annotations dependency to your project.

    1. The JSR356 Manual Way

      Use the javax.websocket.server.ServerApplicationConfig to report the WSCommunication as being available to be added from within the webapp's startup.

    2. The Servlet Spec Manual Way

      This is the easiest approach. Create a javax.servlet.ServletContextListener that adds the WSCommunication endpoint to the ServerContainer

    public class MyContextListener implements ServletContextListener
    {
        @Override
        public void contextDestroyed(ServletContextEvent sce)
        {
        }
    
        @Override
        public void contextInitialized(ServletContextEvent sce)
        {
            ServerContainer container = (ServerContainer)sce.getServletContext()
                       .getAttribute(ServerContainer.class.getName());
    
            try
            {
                container.addEndpoint(WSCommunication.class);
            }
            catch (DeploymentException e)
            {
                throw new RuntimeException("Unable to add endpoint",e);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题