示例#1
0
 public function socket_server($port)
 {
     global $sockets, $groups, $master;
     $main = new Group();
     $main->port = $port;
     $main->name = "Main";
     $main->members = array();
     $main->task = "chat";
     array_push($groups, $main);
     $master = connection::WebSocket($port);
     $sockets = array($master);
     while (true) {
         $changed = $sockets;
         $write = NULL;
         $except = NULL;
         socket_select($changed, $write, $except, NULL);
         foreach ($changed as $socket) {
             if ($socket == $master) {
                 $client_socket = socket_accept($master);
                 if ($client_socket < 0) {
                     continue;
                 } else {
                     connection::connect($client_socket);
                 }
             } else {
                 $client = select::bysocket($socket);
                 $bytes_lenght = socket_recv($socket, $buffer, 2048, 0);
                 if ($bytes_lenght <= 6) {
                     connection::disconnect($client);
                 } else {
                     if ($client->status < 2) {
                         connection::dohandshake($client, $buffer);
                     } else {
                         $buffer = other::handle_data($buffer);
                         if ($client->status < 3) {
                             connection::login($client, $buffer);
                         } else {
                             if (true) {
                                 chat::process($client, $buffer);
                             } else {
                                 //nothing
                             }
                         }
                     }
                 }
             }
         }
         chat::logm("run\n");
     }
 }