Meaningless Drivel is fun!*
The moose likes Tomcat and the fly likes Tomcat 7 is not creating connection for web socket. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Tomcat 7 is not creating connection for web socket." Watch "Tomcat 7 is not creating connection for web socket." New topic
Author

Tomcat 7 is not creating connection for web socket.

sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

Hi all,
I have trouble while building my web Socket connection while using tomcat 7.0.53 server. I have spent hours in finding solution of this and attempted different combination of mentioned solution, unfortunately it didn't work for me. I need help in this regards as i got following error.

Firefox can't establish a connection to the server at ws://localhost:8080/socket/webSocket.

Below is my client side code :



Server Side Code :



I am using ecllispe and javax.websocket-api-1.0.jar is in my project dependency list.

Much needed help to resolve this issue. Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  19

Is this JAR file deployed with your web application? If so, make sure it doesn't. Tomcat 7.0.53 comes with its own JAR file in the lib folder (websocket-api.jar).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

Rob Spoor wrote:Is this JAR file deployed with your web application? If so, make sure it doesn't. Tomcat 7.0.53 comes with its own JAR file in the lib folder (websocket-api.jar).


I have removed it from tomcat lib folder and result is same . Any other suggestion ?
sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

help help help ......
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
Do you really think that this kind of post makes people want to help you? You can use the "Edit" button to give it a more reasonable shape. Also tell us what you have tried since yesterday.


Ping & DNS - my free Android networking tools app
sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

My bad if it seems annoying. People don't see posts after suggesting something. Suggestion from "Rob" didn't solve my problem which make me to do this but surely i will edit it.
I have tried to remove Tomcat websocket-api.jar and tried to change tomcat version like 7.0.29 but same issue. Faced same issue at every try with following message
can't establish a connection to the server at ws://localhost:8080/socket/webSocket.
sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

I have also tried to make connection with following line :

In this case my connection is successful and i see the alert on screen as written in my code pasted above. Obviously it just a test to verify that issue is with my web socket server. In my case my socket end point is "SocketEndPoint " deployed on tomcat 7.0.53.
This is what i had done since yesterday. Now i am blocked on this completely.
My web.xml is as follows which was missed in first code snippet.


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
Make sure that the jar file you're using is not named in a way so it will be skipped when processing annotations. See http://tomcat.apache.org/migration-7.html#Annotation_scanning for what that means, and crosscheck with the jarsToSkip property in conf/catalina.properties.
sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

Dittmer,

Nothing worked. I have ensured that JAR files in tomcat lib for web socket should properly skipped and my connection could established.
sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

My Web.xml for this attempt :




My dependency jar file. Now suggest what else i can do to solve this ?


[Untitled.png]

[Thumbnail for apache.png]

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

Is there anything in the Tomcat logs that show logs related to the application deployment? Can you post that part?

[My Blog] [JavaRanch Journal]
sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

Instead of following lines which logs on server up nothing getting log in tomcat log files.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
I have ensured that JAR files in tomcat lib for web socket should properly skipped and my connection could established.

This sounds as if you have misunderstood the meaning of what I posted. The files in Tomcat's lib folder are irrelevant. What's important is the jar file your class is in (assuming that it is in a jar file). If it is, then it must not be skipped. That means you have to match its name against all the jar files names that will be skipped, and ensure that the name of yours does not match any of those.

When I implemented such an endpoint I had exactly the same problem - and it turned out that my jar file was matching one of the file names being skipped for annotation scans. As soon as I changed the name of the jar file, everything worked. So I think this is something to look at closely: a) are your classes in a jar file (or several)?, and b) if they are, what are the names of those jar files?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
Oh, and you need to remove metadata-complete="true" from your web.xml, as well as the servlet mapping for WsChatServlet (what is that doing there, this isn't a servlet) and the "absolute-ordering" element.

Why is the class extending HttpServlet instead of Endpoint?
sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

Ulf Dittmer wrote:Why is the class extending HttpServlet instead of Endpoint?



Sorry, you are right i miss understood it. There is no particular reason that i am extending my class with HttpServlet and mapping in web.xml. I was just to try different combinations of solution so that my EndPoint annotation could be read. That was just a test. I will surely remove it. Let me try it again and share the results.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
Just to be clear: this is unlikely to fix the underlying problem. You need to investigate the issue about jar file scanning. Why don't you start by giving us some information so we can help you:

So I think this is something to look at closely: a) are your classes in a jar file (or several)?, and b) if they are, what are the names of those jar files?
sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

Ulf Dittmer wrote:You need to investigate the issue about jar file scanning. Why don't you start by giving us some information so we can help you:


Apology if i could miss understood it again. My classes are not under any jar file. They just exist under WEB-INF directory in classes folder. Let me show you my class file structure.


[Thumbnail for Source and class file.png]

sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

My error message changes suddenly. Let me explain what i have done so far.
I have removed servlet entry from my web.xml file and removed extends HttpServlet from my webSocket end point java class.
I had created a jar file of my webSocket end point as suggested and add in my lib folder. I think i am very close to resolve the issue. Now error message is :



Please suggest it.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
A URLs starts with the protocol.
sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

After trying all the mentioned stuff i am standing on first step of ladder.
Same error same issue. Noting is printing in logs. Interestingly i have a network error now along with connection not established.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
Not sure what's happening. Here's a small demo I cobbled together that works fine if I drop it into my Tomcat webapps folder. Source is included.


[Download websocket.war] Download

sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

Thanks Ulf Dittmer. I highly appreciate your interest in resolving my problem. I think it will give my life to resolve the issue and now i got the same problem while running your war file.We have very long thread on the implementation of web socket and i have changed the code as per suggestions. I really thinking that we should should consider Server and environment in it. I have reviewed implementation in war file which is almost same as mine and result is same "Connection failed" . WAR is running in your environment but i am not being able to run that. Ultimately its server responsibility to create web socket. Put some light on server version and your environment where this WAR file is running. Thanks.

Finally the error while running on chrome :


Error on Firefox :

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
In that case I'm out of ideas - that war file deploys and runs fine on my local machine. Your Tomcat log file should show something like "INFO: Deploying web application archive ...../webapps/websocket.war" (and no error message, obviously). Do you see that?
sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

Thanks a lot Ulf Dittmer. Your war file is running fine now. I just reinstall my Tomcat server 7.0.53. Now it will be easy to find issue in my implementation. Once again Thanks for helping me "Ulf Dittmer".
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
Glad to hear you got it to run. Just keep in mind the issue I mentioned earlier about the name of the jar file - because you really should deploy your classes in a jar file, and not as loose class files.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
I'm only just noticing that the "examples" web app that comes with Tomcat 7 contains 4 complete examples of how to use WebSocket, including client and server source code. Too late :-(
sohaib yasir
Ranch Hand

Joined: Mar 10, 2014
Posts: 49

Ulf Dittmer wrote:noticing that the "examples" web app that comes with Tomcat 7 contains 4 complete. Too late :-(


I had noticed and tried it before posting my question. I got the same connection error while running those examples. That's why i thought about to take experts opinion. Luckily example war given by you is running successfully and unfortunately i am still unable to run mine . But i am hopeful that after getting a running war file i will surely run my implementation .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tomcat 7 is not creating connection for web socket.