计算机拒绝 websocket 连接 [英] Computer refuses websocket connections

查看:88
本文介绍了计算机拒绝 websocket 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在测试一个非常基本的套接字示例,但即使是基本测试也不起作用.我的 Python 代码(服务器)是:

I'm currently testing a very basic socket example and even the basic test doesn't work. My Python code (server) is:

import socket
server = socket.socket(socket.AF_INET)
server.bind(("0.0.0.0", 5000))
server.listen(1)
conn, addr = server.accept()

我的 Javascript 代码(客户端)是:

My Javascript code (client) is:

var connection = new WebSocket("wss://<my-local-ip>:5000");

但是,在启动我的 Python 代码然后运行我的 Javascript 代码后,连接在 WebSocket.CONNECTING 状态上挂起大约半分钟,然后出现错误:

However, after starting my Python code and then running my Javascript code, the connection hangs on the WebSocket.CONNECTING state for around half a minute before giving the error:

WebSocket 连接到 'wss://192.168.0.119:5000/' 失败:WebSocket 打开握手超时

我也尝试将 <my-local-ip> 替换为我的公共 IP(根据 ipinfo.io/ip),但这给出了不同的错误:

I also tried replacing <my-local-ip> with my public IP (according to ipinfo.io/ip), but that gave a different error:

WebSocket 连接到 'wss://216.58.126.4:5000/' 失败:连接建立错误:net::ERR_CONNECTION_REFUSED

我查看了互联网,所有示例的结构都与我的测试代码完全相同,为什么它不起作用?

I've looked on the internet and all examples are structured exactly the same way as my test code so why is it not working?

推荐答案

我不确定您问题的第一部分.但是,对于第二部分,您需要通过路由器配置端口转发.

I'm not sure as to the first part of your question. However, for the second part, you'll need to configure port forwarding via your router.

如果您对此不熟悉,它基本上会告诉路由器将任何请求发送到端口 X 到端口 Y 上的某个本地 IP(X 和 Y 通常相同).您自然需要一个静态本地 IP,否则您必须使路由器与计算机的动态 IP 保持同步.

If you're not familiar with that, it'll basically tell the router to send any requests to port X to a certain local IP on port Y (X and Y are commonly the same). You'll want a static local IP, naturally, as otherwise you'd have to keep your router in sync with the dynamic IP of your computer.

有关端口转发的更多信息,请参阅this在超级用户上发布.

For more info on port forwarding, see this post on SuperUser.

这篇关于计算机拒绝 websocket 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆