简体   繁体   English

WebSocket 连接失败:连接建立时出错:.net::ERR_CONNECTION_REFUSED

[英]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.我是 WebRTC 和 WebSockets 的新手,正在按照本教程创建 WebRTC 演示项目,但我无法创建 WebSocket 连接。 I have followed the same steps as mentioned in the project.我遵循了项目中提到的相同步骤。 His project is running on port 8080 and he mentioned ws://localhost:9090.他的项目在端口 8080 上运行,他提到了 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.我的项目运行在 8081 端口,但我复制了他的 URL ws://localhost:9090 因为我不知道 9090 的意义,我收到了这个错误,我的服务器是 node.js。我也将本地主机更改为 8081 但是然后我收到握手错误。

WebSocket connection to 'ws://localhost:9090/' failed: Error in connection establishment:.net::ERR_CONNECTION_REFUSED. WebSocket 与“ws://localhost:9090/”的连接失败:连接建立时出错:.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). Chrome 不允许不安全的 websocket (ws) 连接到本地主机(只有 wss,所以你应该为你的本地 web/websocket 服务器设置一个 TLS 证书)。 However the same should work fine with Firefox.但是,Firefox 应该也能正常工作。

You need to use ws://yourIp:9090/ , where yourIP is like 192.168.?.?您需要使用ws://yourIp:9090/ ,其中 yourIP 类似于192.168.?.? . .

尝试将端口更改为 8080

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

Port 9090 is used by reactotron.端口 9090 由 reactotron 使用。 Probably you are using it in your project and your app cannot connect with reactotron because it is closed.可能你在你的项目中使用它并且你的应用程序无法连接 reactotron 因为它是关闭的。 Just open reactotron and the error will disappear.只需打开reactotron,错误就会消失。

Usually WebRTC requires a secure connection (that is https).通常 WebRTC 需要安全连接(即 https)。 The error you have got is due to TLS/SSL certificates occupied, may be they are not properly configured in your project.您遇到的错误是由于 TLS/SSL 证书被占用,可能是您的项目中未正确配置它们。 Provide a valid TLS/SSL certificate and also configure it correctly in project, then it will work without the above error.提供有效的 TLS/SSL 证书并在项目中正确配置它,然后它将正常工作而不会出现上述错误。

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 .如果有 python 服务器文件,您可以尝试通过 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.我想这是一个通用的 websocket 问题。

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:使用内置的location.host变量将 url 更改为动态名称,如果您已设置 TLS,则更改协议以保护 websocket wss

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 WebSocket 连接到 'ws://localhost:8080/' 失败 必须确保服务器文件正在运行 I git 上述问题

maybe you forgot to start websocket server, check it again, with configuration in my project, run:也许你忘了启动websocket服务器,再检查一遍,在我的项目中配置,运行:

php artisan websocket:init

The error message;错误信息;

"WebSocket connection to 'ws://localhost:9090/' failed: Error in connection establishment:.net::ERR_CONNECTION_REFUSED" “与 'ws://localhost:9090/' 的 WebSocket 连接失败:连接建立时出错:.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.表示您的客户端正在尝试连接到位于ws://localhost:9090的 WebSocket 服务器,但连接被拒绝。 This could be because:这可能是因为:

  1. The WebSocket server is not running on the specified port or IP address . WebSocket 服务器未在指定portIP address上运行。
  2. The WebSocket server is running, but not accepting connections from your client. WebSocket 服务器正在运行,但不接受来自客户端的连接。
  3. There is a firewall blocking the connection.firewall阻止连接。

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.要解决此问题,请确保 WebSocket 服务器在正确的 IP 地址和端口上运行,并且它正在接受来自客户端的连接。 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.此外,您可能需要更新客户端代码中 WebSocket 服务器的 URL,以匹配您尝试连接的服务器的正确 IP 地址和端口号。

Example:例子:

Here is an example of a Node.js client trying to connect to a WebSocket server:这是一个 Node.js 客户端尝试连接到 WebSocket 服务器的示例:

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 .在此示例中,客户端创建一个 WebSocket 连接到位于ws://localhost:9090的服务器。

  • If the connection is established successfully, a message " WebSocket connection established " will be logged to the console.如果成功建立连接,控制台将记录一条消息“ WebSocket connection established ”。
  • 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.如果连接失败,错误信息“ WebSocket connection failed: [error message] ”将被记录到控制台,错误信息指出连接失败的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 chrome 扩展 WebSocket 连接到“ws://localhost:9090/”失败:连接建立错误:net::ERR_CONNECTION_REFUSED - chrome extension WebSocket connection to 'ws://localhost:9090/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED WebSocket 连接到“wss://ip:8080/”失败:连接建立错误:net::ERR_CONNECTION_REFUSED - WebSocket connection to 'wss://ip:8080/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED 失败:连接建立时出错:.net::ERR_CONNECTION_REFUSED - Failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED Websocket连接被拒绝。 WebSocket与'ws://127.0.0.1:2000 /'的连接失败:连接建立错误:net :: ERR_CONNECTION_REFUSED - Websocket connection refused. WebSocket connection to 'ws://127.0.0.1:2000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED 网络套接字 | net::ERR_CONNECTION_REFUSED - WebSocket | net::ERR_CONNECTION_REFUSED 错误选项 net::ERR_CONNECTION_REFUSED - Error OPTIONS net::ERR_CONNECTION_REFUSED 与'ws://。/'的WebSocket连接失败:连接建立错误:net :: ERR_NAME_NOT_RESOLVED - WebSocket connection to 'ws://./' failed: Error in connection establishment: net::ERR_NAME_NOT_RESOLVED Heroku上的WebSocket ERR_CONNECTION_REFUSED - WebSocket on Heroku ERR_CONNECTION_REFUSED WebSocket 连接失败:连接建立错误 - WebSocket Connection failed: error in connection establishment AxiosError:加载资源失败:net::ERR_CONNECTION_REFUSED - AxiosError : Failed to load resource: net::ERR_CONNECTION_REFUSED
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM