32

I try to connect with socket to my server. My server is running server socket with Rachet on port 8080. I try to run this code:

<script>   

    try{
    conn = new WebSocket('wss://localhost:8080');


conn.onclose = function (e)
{
        //checkUser();
}

conn.onopen = function(e) 
{
    console.log("test");
};

    }catch (error)
{
    console.log(error);
}

  </script>

But I get this error:

WebSocket connection to 'wss://localhost:8080/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

When I try to connect from my linux shell I get this:

root@(none):~# telnet localhost 8080
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Nate
  • 26,164
  • 34
  • 130
  • 214
dasdasd
  • 1,971
  • 10
  • 45
  • 72
  • 3
    Is your Ratchet installation configured to support secure connections (`wss:`)? Are your keypair and certificate correctly installed? Note also that self-signed certificates will not be accepted by a WebSocket client; it must be signed by a trusted CA. The fact that your server accepts connections does not mean it is correctly set up to accept *secure* connections. the failure to accept secure connection is a failure on the server; in order to get help, you need to supply more information about how your Ratchet installation is set up. (Or, simply don't use secure connections.) – apsillers Mar 27 '15 at 18:33
  • have you checked the server's firewall settings? – Yonatan Aug 09 '17 at 06:26

5 Answers5

8

CONNECTION_REFUSED is standard when the port is closed, but it could be rejected because SSL is failing authentication (one of a billion reasons). Did you configure SSL with Ratchet? (Apache is bypassed) Did you try without SSL in JavaScript?

I don't think Ratchet has built-in support for SSL. But even if it does you'll want to try the ws:// protocol first; it's a lot simpler, easier to debug, and closer to telnet. Chrome or the socket service may also be generating the REFUSED error if the service doesn't support SSL (because you explicitly requested SSL).

However the refused message is likely a server side problem, (usually port closed).

btraas
  • 160
  • 2
  • 9
4

Firstly, I would try a non-secure websocket connection. So remove one of the s's from the connection address:

conn = new WebSocket('ws://localhost:8080');

If that doesn't work, then the next thing I would check is your server's firewall settings. You need to open port 8080 both in TCP_IN and TCP_OUT.

Nate
  • 26,164
  • 34
  • 130
  • 214
0

In my case the answer is pretty simple. Please check carefully the hardcoded url port: it is 8080. For some reason the value has changed to: for example 3030.

Just refresh the port in your ajax url string to the appropriate one.

conn = new WebSocket('ws://localhost:3030'); //should solve the issue
Anton Lyhin
  • 1,925
  • 2
  • 28
  • 34
0

I had the same issue - the client script ran in a browser on Windows 10, the websocket server ran on WSL 2 (nodejs app using the WebSocketServer package).

Only the localhost or the IPv6 localhost [::1] variant (to which the browser will eventually resolve when localhost is used) will actually correctly connect to WSL 2 and hence to the nodejs websocket server app. IPv4 127.0.0.1 localhost will not work in that case (Connection refused).

You can check that yourself in browser (Chrome): When you request a websocket address, many implementation will respond with Upgrade Required.

strarsis
  • 456
  • 8
  • 18
0

maybe you forgot to start websocket server, check it again, with configuration in my project, run:

php artisan websocket:init