PHP game server, multiple TCP clients? PHP game server, multiple TCP clients? php php

PHP game server, multiple TCP clients?


You really need to run a PHP daemon in order to do this effectively (and it NEEDS to be PHP 5.3). I wrote a fairly completely overview of using PHP for daemon processes. Whatever you chose, I would suggest you use an event based, run loop system.

I've designed a basic RunLoop library called LooPHP which could probably be helpful, especially if your going to be dealing with *_select. I'd be more than happy to answer any question you have about that.

EDIT:

In an event based system you don't simply while a list of commands, you react to a listener. For example...

Instead of doing:

while( 1 ) {    ... /* listen, react */} /* repeat */

Run loops work by registering listener (sockets, and other async event generators)

class ReactClass { ... }$loop = new LooPHP_EventLoop( new ReactClass );//add one time event$loop->addEvent( function() {    print "This event was called 0.5 second after being added\n";}, 0.5 /* in seconds */ );//this creates a repeating event, this is called right away and repeats$add_event = function() use ( $loop, &$add_event ) {    print "This event is REPEATEDLY called 0.1 every second\n";    $loop->addEvent( $add_event, 0.1 );};$add_event();//start the loop processing, no events are processed until this is done$loop->run(); //php doesn't leave this call until the daemon is doneexit(0); //cleanly exit

The above case is a very simple 1 source EventLoop and a manually add timed functions ( these can be added even from within a call to ReactClass).

In the application I'm working I needed to have both asynchronous event feed into the backend (via a socket) and then needed to have the ability to call functions arbitrary offset from the original event (for timed-out clients, etc).

If you'd like more examples, you can find them over at github.

I hope you find this useful.


I wouldn't suggest using PHP for this type of application. PHP doesn't officially support multithreading and running a PHP script for an undefined period of time (like a server) isn't really an advertised feature.

Of course you could try and make history :)

(please correct me if i'm mistaken)