Javascript WebSocket 连接到“ws://localhost:9090/”失败:连接建立错误:net::ERR_CONNECTION_REFUSED

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44575978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:37:35  来源:igfitidea点击:

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

javascriptnode.jswebsocketwebrtc

提问by Hafsa

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.

我是 WebRTC 和 WebSockets 的新手,正在按照本教程创建 WebRTC 演示项目,但我无法创建 WebSocket 连接。我遵循了项目中提到的相同步骤。他的项目在端口 8080 上运行,他提到了 ws://localhost:9090。我的项目在端口 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。

回答by Istvan

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.

Chrome 不允许不安全的 websocket (ws) 连接到本地主机(只有 wss,所以你应该为你的本地 web/websocket 服务器设置一个 TLS 证书)。但是,Firefox 应该也能正常工作。

回答by Ashot Arzumanyan

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

你需要使用ws://yourIp:9090/,你的IP在哪里192.168.?.?

回答by Kasumi Gunasekara

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.

通常 WebRTC 需要安全连接(即 https)。您遇到的错误是由于 TLS/SSL 证书被占用,可能是您的项目中未正确配置它们。提供有效的 TLS/SSL 证书并在项目中正确配置它,然后它将正常工作而不会出现上述错误。

回答by fadi omar

try to change the port to 8080

尝试将端口更改为 8080

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

回答by Jorge Santana

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.

端口 9090 由 reactotron 使用。可能你在你的项目中使用它并且你的应用程序无法连接 reactotron 因为它是关闭的。只需打开reactotron,错误就会消失。