I'm making a web browser based multiplayer game. I've determined that websockets are the best way to handle communications given its realtime nature. The client uses a HTML5 canvas to render the game and websockets to communicate with the host.
I've elected to use PHP for hosting the game as it seems to be preferred by hosting providers. I haven't used PHP before but have done similar things with websockets in Java, but relying heavily on multithreading.
I've been looking at a few tutorials on php sockets with multiple clients; but most of them do things like fork off new processes for each client. Since I'll have a constantly running game loop I don't think this is suitable.
What I'm trying to achieve is a means of assigning ports to each client as they connect, listening for new clients, exchanging data with the current list of clients and running the game loop all together.
The places where I need help are:
Can anyone give me some technical advice on how to achieve these goals? I don't think this all looks like too much to ask of PHP but correct me if I'm wrong!
Some pseudocode of what I'd ideally like to achieve server-side. None of the functions should block: Array clients;
while(gamerunning)
{
CheckForNewClients();
GetStatusFromClients();
DoGameUpdate();
SendGameStateToClients();
}
[Update] For anyone interested, I created a dedicated application supporting web sockets (specifically using Java, and 'TooTallNates' web socket library) rather than an actual web service as it seemed to make more sense, though incidentally it seems most web browsers have since slung web sockets in the bin due to security issues.
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 done
exit(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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With