简体   繁体   中英

WebSocket connection failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

I am new to WebRTC and WebSockets and was following this tutorial to create a WebRTC demo project, but I am unable to create a WebSocket connection. I have followed the same steps as mentioned in the project. His project is running on port 8080 and he mentioned ws://localhost:9090. My project is running on port 8081, but I copied his URL ws://localhost:9090 because I didn't know the significance of 9090 and I received this error and my server is node.js. i changed local host to 8081 as well but then i am getting hand shake error.

WebSocket connection to 'ws://localhost:9090/' failed: Error in connection establishment:.net::ERR_CONNECTION_REFUSED.

Chrome doesn't allow unsecure websocket (ws) connections to localhost (only wss, so you should setup a TLS certificate for your local web/websocket server). However the same should work fine with Firefox.

You need to use ws://yourIp:9090/ , where yourIP is like 192.168.?.? .

尝试将端口更改为 8080

const ws = new WebSocket('ws://localhost:8080/chat')

Port 9090 is used by reactotron. Probably you are using it in your project and your app cannot connect with reactotron because it is closed. Just open reactotron and the error will disappear.

Usually WebRTC requires a secure connection (that is https). The error you have got is due to TLS/SSL certificates occupied, may be they are not properly configured in your project. Provide a valid TLS/SSL certificate and also configure it correctly in project, then it will work without the above error.

Can you make sure if the server is running? If there is a python server file you can try running that server by python3 server.py .

In my case, I forgot to run the server and it was giving me the same error.

I guess this is a generic websocket issue.

Change the url to a dynamic name using the built-in location.host variable and change the protocol to secure websocket wss if you have set-up the TLS:

const ws = new WebSocket("wss://" + location.host + "/")

您也可以轻松更改 IP 地址到主机名的映射,在 Windows 上转到 C:\\Windows\\System32\\drivers\\etc\\hosts 并取消注释此行 127.0.0.1 localhost save 并重新启动。

WebSocket connection to 'ws://localhost:8080/' failed Must ensure server file is running I git this above problem

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

php artisan websocket:init

The error message;

"WebSocket connection to 'ws://localhost:9090/' failed: Error in connection establishment:.net::ERR_CONNECTION_REFUSED"

indicates that your client is trying to connect to a WebSocket server at ws://localhost:9090 but the connection is being refused. This could be because:

  1. The WebSocket server is not running on the specified port or IP address .
  2. The WebSocket server is running, but not accepting connections from your client.
  3. There is a firewall blocking the connection.

To resolve this issue, ensure that the WebSocket server is running on the correct IP address and port , and that it is accepting connections from your client. If you are using a firewall, make sure that the firewall is not blocking the connection . Additionally, you may need to update the URL of the WebSocket server in your client code to match the correct IP address and port number of the server you are trying to connect to.

Example:

Here is an example of a Node.js client trying to connect to a WebSocket server:

const WebSocket = require('ws');

const ws = new WebSocket('ws://localhost:9090');

ws.on('open', function open() {
  console.log('WebSocket connection established');
});

ws.on('error', function error(err) {
  console.error('WebSocket connection failed:', err);
});


In this example, the client creates a WebSocket connection to the server at ws://localhost:9090 .

  • If the connection is established successfully, a message " WebSocket connection established " will be logged to the console.
  • If the connection fails, an error message " WebSocket connection failed: [error message] " will be logged to the console, with the error message indicating the reason for the connection failure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM