php HTML5 Websocket 仅适用于本地主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3488943/
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-25 09:57:43  来源:igfitidea点击:

HTML5 Websocket only works on localhost

phphtmlip-addresslocalhostwebsocket

提问by ssac

I followed the HTML5 Websocket tutorial of the website below:

我按照以下网站的 HTML5 Websocket 教程进行操作:

http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/

http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/

It worked well, then I changed the host value from "localhost:8080" to "218.175.192.160:8080" (my ip) in client-side page, the server side still kept "localhost:8080".

它运行良好,然后我在客户端页面将主机值从“localhost:8080”更改为“218.175.192.160:8080”(我的ip),服务器端仍然保留“localhost:8080”。

It turns out that I can view the page in browser(apache) with http protocol, for example "http://218.175.192.160:8080/client.html", however, the socket in client-side can not connect to "ws://218.175.192.160:8080/daemon.php".(editted typo)

原来我可以用http协议在浏览器(apache)中查看页面,例如“ http://218.175.192.160:8080/client.html”,但是客户端的socket无法连接到“ws ://218.175.192.160:8080/daemon.php”。(编辑错字)

my questions are: 1. in client side, why does websocket only work on "ws://localhost:8080/daemon.php", not "ws://218.175.192.160:8080/daemon.php"?

我的问题是: 1. 在客户端,为什么 websocket 只适用于“ws://localhost:8080/daemon.php”,而不是“ws://218.175.192.160:8080/daemon.php”?

2. why socket_bind( $socket, "127.0.0.1", 8080 ) works, but socket_bind( $socket, "218.175.192.160", 8080 ) occur error? the system reminds "unable to bind address [0]: The requested address is not valid in its context". I am sure that the Ip address belongs to my server.

2. 为什么socket_bind( $socket, "127.0.0.1", 8080 ) 可以工作,但是socket_bind( $socket, "218.175.192.160", 8080 ) 出现错误?系统提示“无法绑定地址[0]:请求的地址在其上下文中无效”。我确定IP地址属于我的服务器。

Please help, Thank you.

请帮忙,谢谢。

I had found out the reason of the error: binding the wrong address, becasue i used router, even though i had set NAT service on router, but i forgot the address of "218.175.192.160" is belonging to the interface of router, not my server machine.

我找到了错误的原因:绑定了错误的地址,因为我使用了路由器,即使我在路由器上设置了NAT服务,但我忘记了“218.175.192.160”的地址是属于路由器的接口,而不是我的服务器机器。

the address of server machine should be the local address type, for example: "192.168.1.2", which is really on the lan card interface.

服务器机器的地址应该是本地地址类型,例如:“192.168.1.2”,这确实是在局域网卡接口上。

sorry that i forgot the basic network setting :( , hope this post helps the network newbie like me, thx~ :)

对不起,我忘记了基本的网络设置:(,希望这篇文章能帮助像我这样的网络新手,谢谢~:)

采纳答案by Moses

I had this problem as well.

我也有这个问题。

The problem is you are most likely referring to your ROUTER's IP address. What you need to do is refer to your COMPUTER's IP address.

问题是您很可能指的是路由器的 IP 地址。您需要做的是参考您的计算机的 IP 地址。

It works for localhost because localhost points to your individual computer and not your router.

它适用于 localhost,因为 localhost 指向您的个人计算机而不是您的路由器。

回答by Arne Roomann-Kurrik

I believe the same origin policy applies to both websockets and server ports, so both client and server have to match or else the connection will fail.

我相信同源策略适用于 websocket 和服务器端口,因此客户端和服务器都必须匹配,否则连接将失败。

In your example, you used:

在您的示例中,您使用了:

http://218.175.192.160:8080/client.html
ws://218.175.192.160/daemon.php

I'm not sure if it was just a typo, but this would need to be

我不确定这是否只是一个错字,但这需要是

http://218.175.192.160:8080/client.html
ws://218.175.192.160:8080/daemon.php

回答by Arne Roomann-Kurrik

Do you still use 'localhost' to initialize your PHP server this way?:

您是否仍然使用 'localhost' 以这种方式初始化您的 PHP 服务器?:

public function __construct($host='localhost',$port=8000,$max=100)  

If you do this then the server will only bind to the localhost interface. Even if you're serving the file correctly from Apache, it appears that the phpwebsockets code still needs to be configured for the external IP.

如果你这样做,那么服务器将只绑定到本地主机接口。即使您从 Apache 正确提供文件,似乎仍然需要为外部 IP 配置 phpwebsockets 代码。

回答by Dave Sag

Instead of socket_bind( $socket, "127.0.0.1", 8080 ), which tells it to only allow requests from localhost, bind to '0.0.0.0' which allows requests from any IP.

而不是socket_bind( $socket, "127.0.0.1", 8080 ),它告诉它只允许来自 的请求localhost,绑定到“0.0.0.0”,它允许来自任何 IP 的请求。

So make it socket_bind( $socket, "0.0.0.0", 8080 )and it will work.

所以做它socket_bind( $socket, "0.0.0.0", 8080 ),它会起作用。