Jump to content

Java Websocket Connection Failure


hhdavidh

Recommended Posts

Hello

 

I've finally been granted Java support on my account, and so today I tried to test out my website/game on it. I'm very new to websites, domains, etc. so my knowledge of all this is limited. That being said I've worked on my website/game for a while now, and I know it works as I have tested it on my computer.

 

 

My game uses a websocket from the client side to connect to the Java websocket endpoint. I use:

 

new WebSocket("ws://localhost:443/Hide/Server/ANY")

 

...on the client side to do this. "localhost" is used because this is being tested on Tomcat on the same computer; "443" because while I can set this to any number (which I have tested), it seems like the best port I could use for a non-secured connection; "Hide" is the project folder name; "Server/ANY" is the server endpoint. I've tested this many times and it works.

 

 

Now to move the project/WAR file (WAR file is called "Hide-1.war") to HelioHost, I first changed the Websocket URL to:

 

new WebSocket("ws://hhdavidh.heliohost.org/Hide/Server/ANY")

 

I then uploaded the WAR file and deployed it. However, when I go to the URL where it's deployed at (http://hhdavidh.heliohost.org/Hide-1/) I get:

 

WebSocket connection to 'ws://hhdavidh.heliohost.org/Hide/Server/ANY' failed: Error during WebSocket handshake: Unexpected response code: 404

 

 

I've tried port "8080" as well as other port numbers. I've tried changing the websocket URL to:

 

new WebSocket("ws://hhdavidh.heliohost.org/Hide-1/Server/ANY")

new WebSocket("ws://hhdavidh.heliohost.org/Server/ANY")

new WebSocket("ws://hhdavidh.heliohost.org:8080/Hide/Server/ANY")

...

 

...which all come up with the same error.

 

 

When trying:

 

new WebSocket("ws://hhdavidh.heliohost.org:443/Hide/Server/ANY")

 

...the error is instead:

 

WebSocket connection to 'ws://hhdavidh.heliohost.org:443/Hide/Server/ANY' failed: Connection closed before receiving a handshake response

 

 

I've tried everything I can think of but now I'm stuck. Any help is appreciated!

 

Link to comment
Share on other sites

You probably can't use port 443 because it's already in use by apache, and you probably can't use port 8080 because it's already in use by tomcat.

 

Yeah, that's what I thought so I tried other port numbers (sorry, I should have made that more clear), but they all still gave the same error.

Link to comment
Share on other sites

The 404 error makes me think that maybe the problem is the url. When our system deploys a .war your username is appended to the front so Hide.war becomes hhdavidh_Hide.war. This causes issues with some applications. Try naming your .war file with your username already on the front so it doesn't get renamed, and deploy that.

Link to comment
Share on other sites

The 404 error makes me think that maybe the problem is the url. When our system deploys a .war your username is appended to the front so Hide.war becomes hhdavidh_Hide.war. This causes issues with some applications. Try naming your .war file with your username already on the front so it doesn't get renamed, and deploy that.

 

I still get an error:

 

WebSocket connection to 'ws://hhdavidh.heliohost.org:1234/hhdavidh_Hide/Server/ANY/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

 

 

Assuming my war file is now called "hhdavidh_Hide.war" what part of this (below) URL should I change for it to be correct?

ws://hhdavidh.heliohost.org:1234/hhdavidh_Hide/Server/ANY/

 

 

Also, I have seen people have their URLs without a port number (:1234). If that's the problem I have, then what port number should my server-side code use?

Link to comment
Share on other sites

Is the WAR file you're running on our server supposed to listen for non-HTTP inbound connections on a custom port? If this what you're doing, you've built a custom server daemon. Custom server daemons are not supported on shared hosting for security reasons.

 

You would need a VPS for that application to work properly. https://heliohost.org/vps/ (The cheapest option would work for a single app, though an extra GB of RAM would be recommended due to Java's memory-hungry nature).

Link to comment
Share on other sites

Is the WAR file you're running on our server supposed to listen for non-HTTP inbound connections on a custom port? If this what you're doing, you've built a custom server daemon. Custom server daemons are not supported on shared hosting for security reasons.

 

You would need a VPS for that application to work properly. https://heliohost.org/vps/ (The cheapest option would work for a single app, though an extra GB of RAM would be recommended due to Java's memory-hungry nature).

 

There is a post on these forums that is quite similar to the problem I have:

 

https://www.helionet.org/index/topic/29870-java-websocket-on-tommy-default-ports-debugging-general-help/

 

...tommygun got a websocket working on Helio, however, there was no mention of a VPS for the websocket to work.

 

Now, I've tried my best to apply the steps tommygun provided in that post, but I'm still having trouble. So I guess my question is 'How would you create a basic websocket connection using a WAR file, and what would be the correct URL for it?'.

Link to comment
Share on other sites

He cheated to make it work. He never actually got the socket to work directly, but rather he made Apache accept and pass the websocket data back to the servlet (Apache can talk to the servlet websocket via localhost even if it's otherwise firewalled in since they run on the same server). That works if the data flowing through the web socket can be handled by Apache...not everything can be. Sockets are supposed to be HTTP compatible during initial connection, so Apache should be able to pass WS data back (effectively making Apache into a proxy). I do know that WSS (secure websocket) never worked.

 

I'd give the method shown there a shot assuming your client can support Apache being in the middle (that's going to depend on what the client is...), but that particular user is no longer on our service so I can't really ask him if he ever got it to fully work.

 

As for VPSes, that's the best practice when building things like this. You have root access on a VPS and can just open the required ports vs. trying to work around the limitations of the shared hosting. Any firewall you'd run into would be one you installed yourself, so you can just add exceptions to make it work.

Link to comment
Share on other sites

He cheated to make it work. He never actually got the socket to work directly, but rather he made Apache accept and pass the websocket data back to the servlet (Apache can talk to the servlet websocket via localhost even if it's otherwise firewalled in since they run on the same server). That works if the data flowing through the web socket can be handled by Apache...not everything can be. Sockets are supposed to be HTTP compatible during initial connection, so Apache should be able to pass WS data back (effectively making Apache into a proxy). I do know that WSS (secure websocket) never worked.

 

I'd give the method shown there a shot assuming your client can support Apache being in the middle (that's going to depend on what the client is...), but that particular user is no longer on our service so I can't really ask him if he ever got it to fully work.

 

As for VPSes, that's the best practice when building things like this. You have root access on a VPS and can just open the required ports vs. trying to work around the limitations of the shared hosting. Any firewall you'd run into would be one you installed yourself, so you can just add exceptions to make it work.

 

Firstly, VPSes aren't really an option for me at the moment as I'm not able to put money into this until after I finished my game/website. Also since I didn't want to use WSS anyway, I've done more research on WS, Apache, .htaccess and using Apache as a proxy. And from what I've learned and from tommygun's post, I've made the following changes:

 

Client (Javascript):

const Connection = new WebSocket("ws://hhdavidh.heliohost.org/Server/ANY");

 

.htaccess File:

RewriteEngine On
RewriteRule "^/Server/ANY$" "ws://localhost:8080/hhdavidh_Hide/Server/ANY" [P,L]

 

Server (Java):

@ServerEndpoint("/Server/{ServerID}")

All of this results in the same error:

WebSocket connection to 'ws://hhdavidh.heliohost.org/Server/ANY' failed: Error during WebSocket handshake: Unexpected response code: 404

On a side note, if I change the .htaccess file to this (as I found in some forums):

RewriteEngine On
RewriteRule "^/Server/ANY$" "ws://localhost:8080/hhdavidh_Hide/Server/ANY" [P,L]

ProxyPass http://localhost:8080/

...then the error changes to this:

WebSocket connection to 'ws://hhdavidh.heliohost.org/Server/ANY' failed: Error during WebSocket handshake: Unexpected response code: 500

Now, regardless if I leave ProxyPass in or out, I still don't know how to replicate tommygun's "non-encrypted websocket chatter".

 

Do you have any more ideas on what I can change or add? This is all new to me so any help would be appreciated.

Link to comment
Share on other sites

 

RewriteRule "^/Server/ANY$" "ws://localhost:8080/hhdavidh_Hide/Server/ANY" [P,L]
Does it work if you add the (.*) like Tommygun did?
RewriteRule ^socket\/(.*) "ws\:\/\/127\.0\.0\.1\:8080\/tommygun_warpaint11\/socket" [P,L]

 

 

I've now changed it to:

RewriteEngine On
RewriteRule "^Server/(.*)" "ws://localhost:8080/hhdavidh_Hide/Server/ANY" [P,L]

...however, it still doesn't work.

 

I should say that the reason my .htaccess looks different to his is that firstly I have tried doing a near copy (apart from necessary adjustments) of his .htaccess file however that still didn't work. And secondly, I tried teaching myself the rules of writing in the .htaccess file yesterday, and from what I've learned I think my .htaccess file should work regardless. I've also removed the RewriteConds so as to not over-complicate it if I make a mistake.

Link to comment
Share on other sites

  • 4 weeks later...

If this guy ever comes back or if someone else is looking for help,

 

I'm connecting to my websocket with

 "ws://tommygun.heliohost.org/socket/"

I noticed he's got 

ws://hhdavidh.heliohost.org/Server/ANY

I'm thinking the next step would be to replicate what I have as closely as possible, so I'd see what could be done about removing the /Server/ part of that url, and I'd also double check that the endpoint is named ANY in the java code.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...