I’m trying to make my PHP server a bit more efficient.
I’ve built an object named Client which contains the connected client (which has an open socket connection with the server) information such as name, id etc.
For now I have one array of socket connections, and one array of Client objects.
When I’m referring a connection, I’m searching inside my Client array to find the right client who matches this connection.
It works great, but it’s a bit inefficient.. For small amount of clients in the server you don’t feel it, but I’m afraid that if I’ll have thousands of connection it will slow down the server.
As a solution I thought about 2 dimensional array, but I have a logic problem designing it.
Can I do something like this:
$clients = array();
$temp = array($newsock, new Client());
$clients[] = $temp;
I want my $clients[] to be the socket and the $clients[][] to be the client object.
In each row of $client I will have only $client[$index][0] which will be my client object for that connection.
Will I be able to send this to the socket_select() function?
You say that you have within your client object an
idattribute. Why not use thatidas the key for both arrays?You might even be able to hold the connection and the client object in one array, each in one object under the same key I talked about before – the clients
id.In any case, wherever you decide to store your clients connection object, you will be able to pass it to all the relevant socket functions –
socket_select();socket_accept();socket_write();With regard to the efficiency of your server, I implemented some forking for broadcasting data to large amounts of clients (all of them in the example of a chat server).
This is the implementation that I used for forking the broadcasts –
The code above was lifted from a previous question of mine (that was self answered).
Terminating zombie child processes forked from socket server
Perhaps it might assist you if you chose to start forking processes.