WebSockets not working without WebApp
Asked Answered
O

2

9

This works:

String webappDir = "...";
context = tomcat.addWebapp("/", new File(webappDir).getAbsolutePath());

This doesn't:

context = tomcat.addContext("/", new File("").getAbsolutePath());

I don't really need a webappDir in this instance because I am not serving any JSP pages or client-side resources, I'm simply using response.getWriter().println(...); server-side only.

There is no exception being thrown, the websocket simply does not open.

Can I assume that this is a tomcat bug?

Optician answered 4/5, 2015 at 22:4 Comment(5)
If you do not need a web application context you can use the Java API for WebSockets JSR 356 directly instead of tomcat websockets API.Thrippence
You can create your client as standalone Java app with JSR 356, to build a server you can use any Java EE server container with websockets support, embedded or not (GlassFish, Apache Tomcat, Jetty...) to deploy a WebSocketServlet and listen for client messages.Thrippence
I think is not possible, the websockets API is designed for using sockets in web applications so you need a container, if you do not need a web application do not use websockets, use simply Java sockets.Thrippence
I have not the habit of talking to myself, the author of the question has removed all his comments, people never ceases to amaze me...Thrippence
@vzamanillo: Your comments are good and need to be read. The show more comments link would not make that possible.Optician
F
3

well, those two are completely different function. If you look at the javadoc for the function addContext, you can see that, you need to setup the context to be able to use websocket. this is retrieved from the api doc.

Add a context - programmatic mode, no default web.xml used. This means that there is no JSP support (no JSP servlet), no default servlet and no web socket support unless explicitly enabled via the programmatic interface.

So, in your case, I guess you can follow the test case in this link how to add an end point into the context.

As for whether you can assume that this is a bug or not. Personally, I don't think this is a bug as the developer itself already mention that they don't provide the web socket connection. But, to make sure, you may contact them and ask ;).

Florinda answered 7/5, 2015 at 16:28 Comment(1)
Of course you can use websocket. What i mean is that you need more effort to set it up as it doesn't come "free". If i have time, I'll try to see your git repo.Florinda
D
1

If you look into the tests (test/org/apache/tomcat/websocket in the source code), they do

tomcat.addContext("", null);

Note that passing null instead of a context path needs a recent Tomcat 8 (no older than several months). The current version is 8.0.22.

Can I assume that this is a tomcat bug?

The rule "before filing a bug" is to ask on the users' mailing list, not on stackoverflow.

Disaffiliate answered 5/5, 2015 at 14:26 Comment(1)
Yes, the tests in 8.0.21 use that API.Disaffiliate

© 2022 - 2024 — McMap. All rights reserved.