WebSocket server in PHP without daemons?

I will try to make my first post here as interesting as possible.

Recently, I was interested in the possibility of processing WebSocket requests on a shared hosting server.

Please do not tell me "update your plan." All of this would be trivial, at least in VPS. I understand that.

As you know, shared hosts will ...

  • Kill the demon if he sees him.
  • Block server socket usage
  • Deny shell access
  • Keep apache restrictions (without installing modules)

These restrictions completely exclude phpwebsocket, python. Requires a no-daemon solution that masquerades as a web page.

PHP is my favorite server language, I created a PHP web gateway, which is a web page.

So far, I have been able to send the correct headers for handshaking and streaming output (using output buffering), but I still cannot figure out how to continue reading data after the initial request.

In short, I want to keep getting data from the client even after running the PHP script. I tried reading the php: // pseudo-file, but I can no longer read it after the end of GET. Are there any settings or hacks that will allow this?

Thanks!

+6
source share
3 answers

short version: what you are trying to do is simply not possible.

long version: the best you can get is an oneway communication channel that looks like a web connection in your browser but works in only one direction. From server to browser. Another direction will simply not work, because the web server does not know that you are trying to use a protocol other than HTTP, and there is no way to report it. At least not in the scenario you just outlined.

+5
source

After the WebSocket handshake is completed, it works almost like regular sockets. There is no reason why Apache allows unidirectional communication without headers.

0
source

Your problem here is in Apache. As soon as Apache reads the first HTTP request (acknowledgment via websocket), it will continue reading from the TCP connection for any additional HTTP requests. This way, any new data sent over a TCP connection will never be passed to your script. This is necessary because HTTP / 1.1 supports Keep-Alive by default, as several request / response cycles are performed over the same TCP connection. The browser does not open an HTTP connection for each request (which was the default in HTTP / 1.0). You cannot change this behavior. To implement the websocket server, you need to configure your own socket.

0
source

Source: https://habr.com/ru/post/914084/


All Articles