tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexander Stöcker <alexander_stoec...@web.de>
Subject Re: Unable to establish WebSocket connection on Tomcat Embed (9.0.24)
Date Mon, 04 Nov 2019 15:09:00 GMT
On Mon, Nov 4, 2019 at 2:55 PM Mark Thomas <markt@apache.org> wrote:
>
> On 04/11/2019 12:59, Alexander Stöcker wrote:
> > My code is basically just:
> >
> >         Tomcat tomcat = new Tomcat();
> >         tomcat.getConnector();
> >         tomcat.setPort(8080);
> >
> >         Context context = tomcat.addContext("/context", new
> > File(".").getAbsolutePath());
> >
> >         // servlet
> >         Tomcat.addServlet(context, "myServlet", new MyServlet());
> >         context.addServletMappingDecoded("/hello", "myServlet");
> >
> >         // websocket
> >         context.addApplicationListener(MyWSContextListener.class.getName());
> >
> >         // run
> >         tomcat.start();
> >         tomcat.getServer().await();
> >
> >
> > The WsContextListener is adding an Endpoint when contextInitialized()
> > is called. Setting a Breakpoint shows that the code is actually
> > executed. The Servlet is working but when I'm trying to connect to the
> > endpoint ("ws://localhost:8080/context/endpoint") I get a 404. On Fine
> > debug output I can see, that this exception is thrown, whenever a
> > connection is requested:
>
> You need to configure the default servlet. Otherwise the mapper looks at
> the URI, sees that it doesn't match any known servlets and rejects the
> request with a 404 before the WebSocket filter has a chance to handle
> the request.
>
> Mark
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>

Indeed, that solved the Problem. I would have never been able to solve
that just from documentation or examples... After some googling on
"default servlet" I found this SO thread which gave me the solution:

https://stackoverflow.com/questions/6349472/embedded-tomcat-not-serving-static-content

TL;DR: You could either write:

        Wrapper defaultServlet = context.createWrapper();
        defaultServlet.setName("default");
        defaultServlet.setServletClass("org.apache.catalina.servlets.DefaultServlet");
        defaultServlet.setLoadOnStartup(1);
        context.addChild(defaultServlet);
        context.addServletMappingDecoded("/", "default");

or  just

        Tomcat.initWebappDefaults(context);

thanks & best regards

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message