1 while (count($clients) > 0) {
2 // create a copy, so $clients doesn't get modified by socket_select()
5 // get a list of all the clients that have data to be read from
6 // if there are no clients with data, go to next iteration
7 $left = @socket_select($read, $write = null, $except = null, 0, 150);
12 // check if there is a client trying to connect
13 if (in_array($mainSocket, $read)) {
14 // accept the client, and add him to the $clients array
15 $new_sock = socket_accept($mainSocket);
16 $clients[] = $new_sock;
18 // send the client a welcome message
19 socket_write($new_sock, "No noobs, but I'll make an exception. :)\n".
20 "There are ".(count($clients) - 1)." client(s) connected to the server.\n");
22 socket_getpeername($new_sock, $ip);
23 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:New client connected: {$ip}");
26 if (count($clients) > 2) {
27 foreach ($clients as $send_sock) {
28 if ($send_sock != $mainSocket && $send_sock != $new_sock) {
29 socket_write($send_sock, "Server: Chatter has joined from {$ip}. There are now ".(count($clients) - 1)." clients.\n");
34 // remove the listening socket from the clients-with-data array
35 $key = array_search($mainSocket, $read);
39 // loop through all the clients that have data to read from
40 foreach ($read as $read_sock) {
42 socket_getpeername($read_sock, $ip);
44 // read until newline or 1024 bytes
45 // socket_read while show errors when the client is disconnected, so silence the error messages
46 $data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);
48 // check if the client is disconnected
49 if (($data === false) || (in_array(strtolower(trim($data)), $leaving))) {
51 // remove client for $clients array
52 $key = array_search($read_sock, $clients);
53 unset($clients[$key]);
54 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client from {$ip} disconnected. Left: ".(count($clients) - 1)."");
57 if (count($clients) > 1) {
58 foreach ($clients as $send_sock) {
59 if ($send_sock != $mainSocket) {
60 socket_write($send_sock, "Server: Chatter from {$ip} has logged out. ".(count($clients) - 1)." client(s) left.\n");
65 // continue to the next client to read from, if any
66 socket_write($read_sock, "Server: Good bye.\n");
67 socket_shutdown($read_sock, 2);
68 socket_close($read_sock);
70 } elseif (in_array(trim($data), $shutdown)) {
71 // Is he allowed to shutdown?
72 if (!in_array($ip, $masters)) {
73 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} has tried to shutdown the server!");
74 socket_write($read_sock, "Server: You are not allowed to shutdown the server!\n");
79 // Close all connections a leave here
80 foreach ($clients as $client) {
81 // Send message to client
82 if ($client !== $mainSocket && $client != $read_sock) {
83 socket_write($client, "Server: Shutting down! Thank you for joining us.\n");
87 socket_shutdown($client, 2);
88 socket_close($client);
97 // trim off the trailing/beginning white spaces
100 // Test for HTML codes
101 $tags = strip_tags($data);
103 // check if there is any data after trimming off the spaces
104 if (!empty($data) && $tags == $data && count($clients) > 2) {
105 // Send confirmation to "chatter"
106 socket_write($read_sock, "\nServer: Message accepted.\n");
108 // send this to all the clients in the $clients array (except the first one, which is a listening socket)
109 foreach ($clients as $send_sock) {
111 // if its the listening sock or the client that we got the message from, go to the next one in the list
112 if ($send_sock == $mainSocket || $send_sock == $read_sock)
115 // write the message to the client -- add a newline character to the end of the message
116 socket_write($send_sock, "{$ip}:{$data}\n");
118 } // end of broadcast foreach
119 } elseif ($tags != $data) {
120 // HTML codes are not allowed
121 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} has entered HTML code!");
122 socket_write($read_sock, "Server: HTML is forbidden!\n");
123 } elseif ((count($clients) == 2) && ($read_sock != $mainSocket)) {
124 // No one else will hear the "chatter"
125 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} speaks with himself.");
126 socket_write($read_sock, "Server: No one will hear you!\n");
128 } // end of reading foreach
131 // close the listening socket
132 socket_close($mainSocket);