Jump to content


Photo

Java Websocket Connection Failure

websocket java johnny javascript port localhost 8080 433 ws wss

  • Please log in to reply
13 replies to this topic

#1 hhdavidh

hhdavidh

    Newbie

  • Members
  • 7 posts

Posted 09 November 2019 - 11:45 PM

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!

 



#2 Krydos

Krydos

    Rank X Member

  • Root Admin
  • 19,162 posts
  • Gender:Male

Posted 10 November 2019 - 01:09 AM

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.

#3 hhdavidh

hhdavidh

    Newbie

  • Members
  • 7 posts

Posted 10 November 2019 - 01:52 AM

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.



#4 Krydos

Krydos

    Rank X Member

  • Root Admin
  • 19,162 posts
  • Gender:Male

Posted 10 November 2019 - 02:33 AM

Which error? You listed two in your original post.

#5 hhdavidh

hhdavidh

    Newbie

  • Members
  • 7 posts

Posted 10 November 2019 - 03:08 AM

Which error? You listed two in your original post.

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



#6 Luigi123

Luigi123

    Rank X Member

  • Moderators
  • 1,888 posts
  • Gender:Male
  • Location:Winnipeg, Manitoba, Canada

Posted 10 November 2019 - 04:57 AM

Moved to Customer Service.
Posted ImagePosted ImagePosted Image
Posted ImagePosted ImagePosted Image

#7 Krydos

Krydos

    Rank X Member

  • Root Admin
  • 19,162 posts
  • Gender:Male

Posted 10 November 2019 - 11:13 PM

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.

#8 hhdavidh

hhdavidh

    Newbie

  • Members
  • 7 posts

Posted 11 November 2019 - 01:16 PM

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?



#9 wolstech

wolstech

    Rank X Member

  • Root Admin
  • 11,172 posts
  • Gender:Male
  • Location:Pennsylvania

Posted 11 November 2019 - 02:42 PM

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


Please do not PM me for support. Posting on our forums lets all of us learn.
Do you play Minecraft? | Server Monitors | And the Dumbass of the Decade award goes to...


#10 hhdavidh

hhdavidh

    Newbie

  • Members
  • 7 posts

Posted 12 November 2019 - 12:55 PM

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...g-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?'.



#11 wolstech

wolstech

    Rank X Member

  • Root Admin
  • 11,172 posts
  • Gender:Male
  • Location:Pennsylvania

Posted 12 November 2019 - 02:42 PM

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.


Please do not PM me for support. Posting on our forums lets all of us learn.
Do you play Minecraft? | Server Monitors | And the Dumbass of the Decade award goes to...


#12 hhdavidh

hhdavidh

    Newbie

  • Members
  • 7 posts

Posted 13 November 2019 - 01:21 AM

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.



#13 Krydos

Krydos

    Rank X Member

  • Root Admin
  • 19,162 posts
  • Gender:Male

Posted 13 November 2019 - 03:12 AM

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]



#14 hhdavidh

hhdavidh

    Newbie

  • Members
  • 7 posts

Posted 13 November 2019 - 12:44 PM

 

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.







Also tagged with one or more of these keywords: websocket, java, johnny, javascript, port, localhost, 8080, 433, ws, wss

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users