• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Web Socket not working in Tomcat 8.0.32

 
Suman Mummaneni
Ranch Hand
Posts: 96
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I need help on WebSockets that I using Tomcat 8.0.32 with WebSocket 1.1. i am not able to get any error on the Server. I am not really sure what is going wrong. Looks like I am missing some think.

Server code
--------------


Client Java Script
--------------------
 
Rob Spoor
Sheriff
Pie
Posts: 20745
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your application the root application? If not, you need to put the context path in the JavaScript websocket endpoint.
 
Suman Mummaneni
Ranch Hand
Posts: 96
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please correct me if I am wrong this is my first websocket application, is the context same as Servlets? Should I modify the xml configuration to make it root? I thought the Annotation will take care of it.
--
Regards
Suman Mummaneni
 
Rob Spoor
Sheriff
Pie
Posts: 20745
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The context path is the same as for servlets. If you have a servlet with URL pattern "myservlet" and you need to put the context path between the port (8080) and the URL pattern, then you must do the same for websocket endpoints in the same WAR. The annotation only makes sure the endpoint is available within the WAR's context path. Unless that context path is empty (the ROOT war), you need the context path in your websocket URL in the JavaScript code.
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just in case there's some misunderstanding, Rob wasn't talking about the servlet definition in the web.xml file.

Tomcat can host multiple webapps, and each one is accessed under a unique context path that forms part of the request URL. There's the root context ("/") and then there are named contexts, whose pathnames generally correspond to the WAR name for simple deployments or the the name of the Context xml file when deploying via the TOMCAT_HOME/conf/Catalina/localhost mechanism.

The root context is exactly the same as any other, except that its WAR is located in the TOMCAT_HOME/webapps/ROOT directory. Usually Tomcat comes with a webapp already installed there. If you wanted to base your websoocket webapp at the root URL, you'd have to replace that WAR with your own webapp's WAR. Don't try to just dump stuff in - it's not likely to do what you want.

Assuming, however that you're deploying under a non-root context - for example, "mywebapp", then your websocket URL would be in the form: ""ws://localhost:8080//mywebapp/socketapp"". Where "ws:" indicates the webservice protocol, localhost:8080 indicates the host and TCP port number, "mywebapp" is your webapp context (corresponding to TOMCAT_HOME/webapps/mywebapp) and "socketapp" is the endpoint that you indicated in your annotation.
 
Suman Mummaneni
Ranch Hand
Posts: 96
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your response. I have configured the Tomcat server and tried invoking the example applications for Web Sockets. I see all of them are working fine. I think there is some problem with the context path for my application. I need figure this out, as Tim mentioned the WS (Web Service) path differs for each application. I think I need to figure out the exact path that needs to be invoked for my sample Web Socket application.

--
Regards
Suman Mummaneni
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic