I'm using websockets for the first time and am unsure what I am doing wrong. Due to my IP being dynamic, I am using the following websocket call serverside:
$echo = new echoServer("myurl.com","9000");
and the following call clientside:
socket = new WebSocket("wss://myurl.com:9000");
I get a timeout err even though the server is completely unoccupied. Does anyone know why?I feel this is a basic error
Best How To :
That specific server, PHP-Websockets, does not provide support for the
wss:// protocol (secure connection, similar to
https:// for HTTP traffic).
Change the connection line to:
socket = new WebSocket("ws://myurl.com:9000");
Alternatively, if you need TLS support, PHP-Websockets might not be the best server for you.
I made the decision to not support TLS in PHP-Websockets because I did not know of any robustly tested, battle hardened PHP packages that provide access to the OpenSSL libraries. I have changed my mind as of 5 minutes ago, and will implement TLS using the built in PHP OpenSSL functions, but this will take time to implement and will require several testers. If you don't want to wait, don't wait.
I do not want people to think that their data is secure when I simply can't guarantee it.
Also, not specifically causing a failure but still an issue: In your server-side code, you should change instantiating the server's object to:
$echo = new echoServer("0.0.0.0", "9000");
The reason is that the IP address is setting the server's listen address. The listen address of "0.0.0.0" gets translated to "listen on all IP addresses that this machine can listen to." That means that, besides just your own public IP, it will also listen to 127.0.0.1, and if you have a second network card that has a different IP address assigned to it, you can receive traffic from that as well.
However, because of PHP's weak typing, when
socket_bind() attempts to convert the IP address into its binary representation, but finds a string instead, it silently converts it to 0, which is equivalent to the IP address 0.0.0.0.
(I previously thought that
socket_bind() was performing a DNS lookup to get the IP address. I was very wrong. Such weak typing on the part of PHP, and especially the assumptions made by developers like me, is the cause of many security holes.)