Websocket JSR-356 fail with Jetty 9.4.1

[亡魂溺海] 提交于 2019-11-28 13:10:38

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);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!