0

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?

  • That's not a websocket server. It is a socket server. Websockets are not the same as sockets. And since your server does not adhere to the Websocket protocol you get the problem with nginx. – Steffen Ullrich Jun 30 '21 at 06:25
  • Sorry, seems you was closed before I could answer, first of all, if you need world-wide access, use `0.0.0.0` IP instead of `127.0.0.1` IP - Finally, consider using one single line `socket_recvfrom($socket, $clientPacket, 1024 /* maxLength */, 0, $clientAddress, $clientPort);` (instead of your 3 calls `socket_listen(...)`, `socket_accept(...)` and `socket_read(...)`) which creates the 3 `client` prefixed variables for you automatically. – Top-Master Jun 30 '21 at 06:45
  • Reply like `socket_sendto($socket, $myReply, strlen(myReply), 0, $clientAddress, $clientPort);` – Top-Master Jun 30 '21 at 06:51
  • @Top-Master I'm new to this topic. So I preferred writing three different lines – Sudarshan Regmi Jun 30 '21 at 06:56
  • Okay; all together this is still just a raw `UDP` socket and/or connection, but as you need `WebSocket` protocol support (i.e. handling `$clientPacket` bytes correctly, and sending proper response), see [related-WebSocket-example](https://github.com/ghedipunk/PHP-Websockets/blob/master/websockets.php) too – Top-Master Jun 30 '21 at 07:11
  • @Top-Master for chat like applications. Can't i use just socketks. Instead of WebSocket. Can't we transfer data over socket? – Sudarshan Regmi Jun 30 '21 at 07:45
  • As long as you are the developer of both Client and Server, you can of course use whatever protocol you want, like, you don't even need to use `TCP` if both sides somehow check `UDP` response (and handle packet loss). But ensure to add packet-encryption support later (once both sides are stable). – Top-Master Jun 30 '21 at 08:13
  • The only problem is that the Web-Browser does not allow `JavaScript` to access the socket API, I mean, if your client is a Web-Site (running on some 3rd-party browser) it's not possible, but anywhere else (Java, Swift, Obj-C, C++ and ...), socket is available. – Top-Master Jun 30 '21 at 08:21

0 Answers0