开发者

WebSocket client can only connect to my server on localhost

开发者 https://www.devze.com 2023-04-11 13:45 出处:网络
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 com

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_I开发者_StackOverflow中文版NET, 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>


Have you tried binding to 0.0.0.0 ?

sock.bind(("0.0.0.0", 12345))


For me, the problem was that I had not opened the firewall for incoming connections to python.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号