WebSocket 客户端只能连接到本地主机上的我的服务器
我一直在玩网络套接字,并且制作了一个小客户端,并将其发布在网站上。当我尝试从网站(从我的计算机)连接到同一台计算机上的简单 Python 服务器时,它工作正常,连接到本地主机。但是我无法从另一台计算机连接。我在这里的很多问题中都看到了这个问题,但没有解决方案。根据此网站,问题可能无法解决。
另一种选择是socket.io,显然,我如何将它用于网络主机上的客户端? GitHub socket.io 页面在安装方面相当模糊,至少对于像我这样的人来说是这样。
一个在本地工作的简单例子是:
# Python server
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("", 12345))
sock.listen(5)
newsock, addr = sock.accept()
print "Connection"
// Client
<script type = "text/javascript">
window.onload = function()
{
// Works
var sock = new WebSocket("ws://127.0.0.1:12345");
// Doesn't work
var sock = new WebSocket("ws://194.237.*.*:12345");
}
</script>
I've been playing with websockets, and I've made a little client that I've put up on a website. When I try to connect from the website ( from my computer ) to a simple Python server on the same computer it works fine, connecting to localhost. However I cannot connect from another computer. I've seen this problem in quite a few questions here, but no solutions. According to this website the problem probably can't be solved.
An alternative is socket.io, apparently, how can I use this for a client on a webhost? The GitHub socket.io page is quite vague on installation, at least for someone like me.
Well a simple example which works locally would be;
# Python server
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("", 12345))
sock.listen(5)
newsock, addr = sock.accept()
print "Connection"
// Client
<script type = "text/javascript">
window.onload = function()
{
// Works
var sock = new WebSocket("ws://127.0.0.1:12345");
// Doesn't work
var sock = new WebSocket("ws://194.237.*.*:12345");
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过绑定到
0.0.0.0
?Have you tried binding to
0.0.0.0
?对我来说,问题是我没有打开 python 传入连接的防火墙。
For me, the problem was that I had not opened the firewall for incoming connections to python.