Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSockets not working without WebApp

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?

like image 284
ThreaT Avatar asked May 04 '15 22:05

ThreaT


People also ask

Does WebSocket require web server?

By definition websockets like normal sockets are client-server so yes, you need a server.

How do I enable WebSockets?

- In Control Panel, click Programs and Features, and then click Turn Windows features on or off. Expand Internet Information Services, expand World Wide Web Services, expand Application Development Features, and then select WebSocket Protocol. Click OK. Click Close.

Are WebSockets deprecated?

Websockets are largely obsolete because nowadays, if you create a HTTP/2 fetch request, any existing keepalive connection to that server is used, so the overhead that pre-HTTP/2 XHR connections needed is lost and with it the advantage of Websockets.

Why you should not use WebSocket?

Avoid using WebSockets if only a small number of messages will be sent or if the messaging is very infrequent. Unless the client must quickly receive or act upon updates, maintaining the open connection may be an unnecessary waste of resources.


Video Answer


2 Answers

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 ;).

like image 152
kucing_terbang Avatar answered Oct 02 '22 15:10

kucing_terbang


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.

like image 23
Konstantin Kolinko Avatar answered Oct 02 '22 14:10

Konstantin Kolinko