开发者

HTML5 Websocket only works on localhost

开发者 https://www.devze.com 2023-01-11 21:14 出处:网络
I followed the HTML5 Websocket tutorial of the website below: http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/

I followed the HTML5 Websocket tutorial of the website below:

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".

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)

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"?

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.

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.

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.

sorry that i forgot the basic network se开发者_C百科tting :( , hope this post helps the network newbie like me, thx~ :)


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.

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


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.

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


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.

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


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

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.

0

精彩评论

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