I’m trying to learn how to make a chat with a socket server.
I noticed everybody uses the same code (ripoff from the zend developer zone).
The problem is no one really explains how it works. Especially the cryptic code after while(true) { .
This would benefit many so i hope someone could take the time and explain the code in detail (DETAIL!).
I’ll answer it myselfe. I went over it line by line.. this is how it works (I’m only explaining the part in the while(true) loops.
1.
This asings freshly created connections to the $read array to be watched for incoming data.
Watches the $read array for new data (I’m still a bit unclear how this works)
Determines when a new connection is being made, than finds an empty spot in the $client array and add the socket.
This next part I’ll split up for easier explanation.
Loops through all the $client array but only works on the ones that actually have a connection.
First it sees if there is still an active connection, if not it closes it.
If there is a connection it read the data, if there is noone this is code for a disconnect.
If there is data it passes it along to other clients (but itselfe).
That’s ti. Hope I got it right.