0

I have one PHP server that is listening to "messages" from some remote PHP clients. Once a message is received by the server, The server is supposed to update a GUI using javascript web sockets on localhost. The PHP server is receiving messages correctly. The problem is when the server attempts to send the message to the Javascript client, I keep getting the error "Firefox can’t establish a connection to the server at ws://127.0.0.1:6002/." and error code 1006.

PHP Server code.

// socket used between PHP clients and PHP server
$srv_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($srv_socket, SOL_SOCKET, SO_REUSEADDR, TRUE);
socket_bind($srv_socket, "10.10.10.2", 6001);
socket_listen($srv_socket);

// socket used between PHP server and Web GUI (javascript)
$gui_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($gui_socket, SOL_SOCKET, SO_REUSEADDR, TRUE);
socket_bind($gui_socket, "127.0.0.1", 6002);
socket_listen($gui_socket);

while (TRUE) {

    // get message from PHP client
    $client_socket = socket_accept($srv_socket);
    $message = socket_read($client_socket, 250);
    socket_close($client_socket);
    if ($message != NULL) {

        echo "Message Received: " . $message . ".";

        // send message to GUI
        $gui_client_socket = socket_accept($gui_socket);
        socket_write($gui_client_socket, $message, strlen($message));
        echo "Message Sent to GUI.";

    }

}

Javascript Client code.

$(document).ready(function(){
    var socket = new WebSocket("ws://127.0.0.1:6002/"); 
    socket.onopen = function(ev) {
        alert('socket connection opened properly');
    };
    socket.onmessage = function(ev) {
        var message = ev.data;
        alert("message arrived!");
    };
    socket.onerror = function(error){
        alert("Error: " + error.message);
    };
    socket.onclose = function(ev){
        alert("Connection closed: " + ev.code);
    }; 
});

The output from the PHP server is Message Received: {"msg":"Hello World!"}. Message Sent to GUI.

The output from Javascript is two alerts: 1st alert: "Error: undefined" 2nd alert: "Connection closed: 1006"

Please NOTE I am running Debian, and I have flushed my iptables with iptables -F and then added two rules for ports 6001 and 6002.

iptables -A INPUT -p tcp -m tcp --dport 6001 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 6002 -j ACCEPT

IMPORTANT I am running the PHP server from terminal and not from Apache. example: php -q /var/www/html/php_server.php, while the Javascript client is attempting to open a websocket on ws://localhost:6002/

Am I missing something? Am doing doing something wrong? Ultimately, I need that javascript client to receive the message from the PHP server. Any help will be highly appreciated, thank you.

camaron182
  • 159
  • 1
  • 6
  • Does this answer your question? [getting the reason why websockets closed with close code 1006](https://stackoverflow.com/questions/19304157/getting-the-reason-why-websockets-closed-with-close-code-1006) – Triby May 03 '20 at 06:12
  • I think the link provides a well-detailed description of the 1006 error, however it does not point to a specific reason other than "failed to open socket". I was hoping somebody that has struggled with the same issue could share some light on this issue. Thanks for your response. – camaron182 May 03 '20 at 06:19
  • This is the first time I see one socket server listening on 2 ports. I'd try to setup and test only one and go for the second after the first is working fine. – Triby May 03 '20 at 06:28
  • Thanks for your answer, it is also my first time implementing a PHP server. I have debug plenty of times already, I know that if I change the javascript web socket to ws://echo.websocket.org, it will work just fine. I trying to find what the problem is. – camaron182 May 03 '20 at 06:36
  • Do you get the `socket connection opened properly` alert? If no, server is the problem. I didn't notice before, but you're closing socket and then try to write to it. – Triby May 03 '20 at 06:42
  • Well, I have two types of clients, that is why I had two ports in the first place. I can receive message from other php clients, and those are the sockets that I close once I receive their message. The problem is with the second kind of client, which is a GUI, I want to send that message that the server just got to it, that socket I never close, and sadly it does not open either, so no "socket connection opened properly" alert. – camaron182 May 03 '20 at 06:46

1 Answers1

0

I found the answer. I was missing the handshake headers when establishing web socket with Javascript client. Please refer to this question and look for the PHP - Server code on the question; since it is the proper way to create a web socket between Javascript client and PHP server and solved my problem.

camaron182
  • 159
  • 1
  • 6