public function onMasterStop($server) { @unlink($this->pid_file); if (APP_DEBUG) { Console::println("server is stop"); } \Pinst::$app->shutdown(); }
public function onMessage($server, $connection, $data) { if (APP_DEBUG) { Console::println("receive client[{$connection->getId}()] message,message data is:\n<<<\n{$data}\n>>>"); } $data = json_decode($data, true); if (empty($data)) { return false; } $action = $data['action']; switch ($action) { case "login": $this->doLogin($connection, $data); break; case "message": \Pinst::$app->server->broadcast($this->buildMessage($data['data'], "message", $connection->getProperty("name")), $connection->getId()); break; case "": break; default: break; } }
/** * when connection close callback * @param \swoole_server $server * @param $client_id * @param $from_id */ public function onClose(\swoole_server $server, $client_id, $from_id) { if (!$this->beforeClose($server, $client_id, $from_id)) { return; } if (APP_DEBUG) { Console::println("client[{$client_id}] close connection!"); } $this->afterClose($server, $client_id, $from_id); }
/** * run application */ public function run() { global $argv; $server_pid = null; $pid_file = $this->server->pid_file; if (file_exists($pid_file)) { $server_pid = intval(@file_get_contents($pid_file)); } if (empty($argv[1]) || $argv[1] == 'start') { if ($server_pid && !empty($pid_file) && $this->isRun($server_pid)) { Console::println("server is running , please stop first!"); return true; } Console::println("start...."); $this->start(); } elseif ($argv[1] == 'stop') { if (empty($server_pid)) { Console::println("Server is not running\n"); } Console::println("stop..."); posix_kill($server_pid, SIGTERM); while ($this->isRun($server_pid)) { sleep(1); } Console::println("server stoped!"); exit; } elseif ($argv[1] == 'restart' || $argv[1] == 'reload') { if (empty($server_pid)) { Console::println("Server is not running\n"); } Console::println("restart..."); posix_kill($server_pid, SIGUSR1); } elseif ($argv[1] == 'state' || $argv[1] == 'status') { if ($server_pid && $this->isRun($server_pid)) { Console::println("Server is running"); } else { Console::println("Server is stop"); } } else { exit("Usage: php {$argv[0]} \n*start start server\n*stop stop server\n*reload|restart restart server \n*state|status get current server status\n"); } }