I'm trying to make a websocket server in php. But I'm getting HTTP 504 gateway timeout error message on nginx/1.18.0. I'm using php7.4 My code to make server:
<?php
echo "<h1> PHP Socket Server </h1>";
#AIM: To develop make a server that reverse the string sent by client using socket programming
#step 1: Set variable such as host and port
$host = "127.0.0.1";
$port = "1500";
// no timeout
set_time_limit(0);
#step 2: Create a socket
$socket = socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create a server <br>");
#step 3: bind socket to the port and host
$bind_result = socket_bind($socket,$host,$port);
#step 4: start listening to the socket
$listen = socket_listen($socket,3) or die("Could not set up socket listener <br>");
#step 5: accept incoming connection
$spawn = socket_accept($socket) or die("Could not accept incoming connection<br>");
// Here $span is that socket resource which is actually responsive for communcation with client socket
#step 6: read the message from client socket
$message = socket_read($spawn,1024) or die("Cound not read message from client socket <br>");
#step 7: reverse the message
$reply = strrev($message);
#step 8: sending message to the client socket
socket_write($spawn,$reply,strlen($reply)) or die("Could not reply to the client socket <br>");
#step 9: close the socket
socket_close($spawn);
socket_close($socket);
?>
What's wrong here? What am I missing? Do I have to install any extensions in php..... How can I fix this issue?