示例#1
0
 public function connect(Dog_Server $server, $blocking = 0)
 {
     if ($this->connecting) {
         return $this->socket_try;
     }
     $this->connecting = true;
     // Disconnect, so the child process has no db that it could kill.
     global $SINGLE_GDO_DB;
     $SINGLE_GDO_DB->disconnect();
     $SINGLE_GDO_DB = null;
     $pid = pcntl_fork();
     if ($pid == -1) {
     } else {
         if ($pid) {
             $this->server = $server;
             self::$PARENT_PID = $pid;
             if (!$this->initQueues()) {
                 die('Cannot get message queue :(');
             }
             $this->connected = true;
             gdo_db();
             return true;
         } else {
             $this->server = $server;
             // we are the child
             declare (ticks=1);
             if (false === pcntl_signal(SIGINT, array($this, 'SIGINT'))) {
                 die('Cannot install SIGINT handler in ' . __FILE__ . PHP_EOL);
             }
             if (false === pcntl_signal(SIGCHLD, array($this, 'SIGINT'))) {
                 die('Cannot install SIGCHLD handler in ' . __FILE__ . PHP_EOL);
             }
             if (!$this->initQueues()) {
                 die('Cannot get message queue :(');
             }
             $this->socket = new WebSocketServer('tcp://0.0.0.0:' . $server->getPort(), 'rob_hubbard_fanclub!');
             $this->socket->addObserver($this);
             $this->socket->initQueue((int) $server->getID());
             // 			sleep(10);
             // 			die('oops');
             if (false === $this->socket->run($this)) {
                 $this->socket_try = false;
                 return false;
             }
             return true;
         }
     }
 }
示例#2
0
        }
    } else {
        $server->send($user->socket, json_encode(array('funct' => 'loginConfirm', 'stat' => 'exist')));
    }
}
function myError($user, $request, $response, $function)
{
    $response->send($user->socket, json_encode(array('stat' => 400, 'function' => $function)));
}
function controller($user, $request, $response)
{
    try {
        $functions = array('login', 'connect', 'sendMove', 'win');
        $data = json_decode($request, true);
        if (empty($data)) {
            myError($user, $request, $response, 'invalidJSON');
        } else {
            if (!in_array($data['funct'], $functions)) {
                myError($user, $request, $response, 'notFound');
            } else {
                call_user_func($data['funct'], $user, $data, $response);
            }
        }
    } catch (Exception $e) {
        myError($user, $request, $response, 'unknownError');
    }
}
require_once 'socketserver.php';
$webSocket = new WebSocketServer("192.168.1.111", 8083, 'controller');
$webSocket->run();