title: PHP game server, multiple TCP clients?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>The places where I need help are:</p>
  
  <ul>
  <li>How to find and assign ports to new clients, notify the client of that port, and clean it up when they disconnect.</li>
  <li>How to do the above, and all other socket transactions, without blocking the game loop. It would be acceptable to accept messages from clients in partial chunks and only act upon a complete message.</li>
  </ul>
  
  <p>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!</p>
  
  <p>Some pseudocode of what I'd ideally like to achieve server-side. None of the functions should block:
      Array clients;</p>
  
  <pre><code>while(gamerunning)
  {
      CheckForNewClients();
      GetStatusFromClients();
      DoGameUpdate();
      SendGameStateToClients();
  }
  </code></pre>
  
  <p>[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.</p>