WebSocket Error 404

I am trying to deploy WebSocket on my site. After the main lessons, I added this class:

@ServerEndpoint(value="/websocketendpoint") public class WebSocket { private static Set<Session> peers = Collections.synchronizedSet(new HashSet<Session>()); @OnMessage public String onMessage(String message){ return null; } @OnOpen public void onOpen(Session peer){ peers.add(peer); } @OnClose public void onClose(Session peer){ peers.remove(peer); } } 

And this is JS:

 var wsUri = "ws://" + document.location.host + document.location.pathname + "websocketendpoint"; var ws = new WebSocket(wsUri); ws.onopen = function(){ ws.send("Message to send"); alert("Message is sent..."); }; ws.onmessage = function (evt){ var received_msg = evt.data; alert("Message is received..."); }; ws.onclose = function(){ alert("Connection is closed..."); }; 

I added js to one of my pages and only ws.onclose , in the console I get this error:

Firefox : Firefox cannot establish a server connection on WS: // local: 8080 / MySITE / websocketendpoint

Chrome : WebSocket handshake error: Unexpected response code: 404

I tried using 'ws: //echo.websocket.org' as wsUri and it works, so I think the problem is on the server side.

I use the following libraries: javaee-api-7.0, javax.websocket-api-1.0 My browsers are compatible with websockets (I checked)

These topics did not help me, so I ask you to explain how to fix this problem.

+2
source share
4 answers

You might want to check if this is the root of your error: tomcat 7.0.50 java webscoket implementation gives 404 errors

Tomcat has an implementation of JSR-356 (i.e. the web interface API) since version 7.0.47. It also provides the javax.websocket-api-1.0.jar , so this is not needed in your application. This may be required at compile time, but the server will provide it at run time (which means provided scope in maven if you are not familiar with the tool)

Make sure javax.websocket-api-1.0.jar not deployed in your WAR, right-click on your Eclipse server, Clean... and then Clean Tomcat work directory... , then Publish and try again.

+7
source

I ran into this problem and decided to use it with tomcat7-websocket.jar instead of javax.websocket-api.jar. Use this dependency:

<dependency org = "org.apache.tomcat" name = "tomcat7-websocket" rev = "7.0.52" />

or put the tomcat7-websocket.jar file in / usr / share / tomcat7 / lib /

+1
source

I came across the same set of symptoms that had a different reason that people, landing on this question, might know. Everything worked fine locally, but failed to run 404 on tomcat and the same error messages as this question when the server was deployed to AWS. The problem was that in AWS, my web socket was transferred through the Apache httpd interface using the ProxyPass directives.

 <Location "/myApp"> AuthType Basic AuthName "Authorized Users Only" AuthBasicProvider file AuthUserFile "/usr/local/secure/htpasswd/passwords" Require user my_user ProxyPass http://localhost:8080/myApp ProxyPassReverse http://localhost:8080/myApp </Location> 

However, this led to the request being turned into an http:// request not a ws:// request when it was passed to tomcat.

The solution was to write a more specific Location , which applies only to websocket.

 <Location "/myApp/socket"> AuthType Basic AuthName "Authorized Users Only" AuthBasicProvider file AuthUserFile "/usr/local/secure/htpasswd/passwords" Require user my_user ProxyPass ws://localhost:8080/myApp/socket ProxyPassReverse ws://localhost:8080/myApp/socket </Location> 
0
source

I suffered from this problem for several days.

I used tomcat7. I tried adding jars to jvm but it doesn't make sense.

Finally, I updated the tomcat8.5 link here and worked like a charm!

0
source

Source: https://habr.com/ru/post/1236942/


All Articles