Php-Flash communication over sockets

812 views Asked by At

Good time of the day, dear developers!

I am not any kind of network programming pro, but it happened that I have faced necessity to develop socket-server on php (no way for using Java) for flash multiplayer browser-game (standard features like locations, team battles, etc).


The main problem is that TCP is point-to-point protocol and it completely occupies given port.

Of course it is possible to create some kind of queue, which will manage connections to the socket, but this solution doesn't seems to be the fittest one.

It seems to me that using interval of "fair" ports (from 2000 to 2200, for example) is more fitting solution, because one request may take a lot of time to execute and players won't be happy to wait in queue.

But how can I implement this "port inteval" strategy? The solutions that I see are:

  • launcing php-script per every port (he-he, 2 hundreds of launched scripts!);

  • somehow forking the initial process to new processes (2 hundreds of processed? Not nice too), one for every port;

  • additional while-loop, which listens all the ports (looks very bad);
  • using threading or something like that (the problem is that php is single-threaded, as far as I know; pcntl?).

But somehow I don't like any of them, or at least don't know how to implement them in the best possible way.

What is the best existing strategy to handle multiple requests from multiple users per time unit without delays, and how to implement this strategy in php? We have our own Debian-server, so it is possible to use any required php extensions.

Any advice about development, planning and implementation of such kind of systems is highlhy appreciated.

Thank You!


Edit 1: By the way, I've forgotten to mention some extra details.

For example, if we are trying to develop chat application, we need some sort of fixed (I mean persistent) connections for each user. For example, we have 80 users in chat, and then one of them posted a message, which server tries to handle and send to all other connected users, also putting an entry to the history file or something like that.

In this situation polling server for new messages every 10 seconds from each of 80 users is craziness, so the need in persistent connection gets obvious. But I don't know what is the best way to implement something like this, considering that not all requests are handled instanlty.

2

There are 2 answers

2
Daniel On
2
Maciek Sawicki On

Best way for multi user communication for Flash is RTMP. Look for FMS or Red5. It's not in php, but I think this is correct way.