diff --git a/README.md b/README.md index 1fd457f..ff408cf 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ location /ws { proxy_set_header Connection 'Upgrade'; } ``` -test connection to websocket +test connection to websocket using blow code or in these website: 1. https://websocketking.com/ 2. http://www.websocket-test.com/ ``` var ws = new WebSocket('ws://127.0.0.1:7000/ws') diff --git a/config/workerman.php b/config/workerman.php index b6b20ff..64d0a6c 100644 --- a/config/workerman.php +++ b/config/workerman.php @@ -70,7 +70,7 @@ 'host' => '0.0.0.0', 'port' => 7400, 'name' => env('APP_NAME', 'laravel-workerman') . ' BusinessWorker', - 'count' => cpu_count() * 2, + 'count' => 1 || cpu_count() * 2, // 每个进程相当于一个游戏大区的概念,默认 1 个进程 'registerAddress' => '127.0.0.1:7100', 'onWorkerStart' => null, 'onWorkerStop' => null, diff --git a/src/Commands/stubs/CustomProcess.stub b/src/Commands/stubs/CustomProcess.stub index 996198b..74d4ea1 100644 --- a/src/Commands/stubs/CustomProcess.stub +++ b/src/Commands/stubs/CustomProcess.stub @@ -14,24 +14,24 @@ class {{ class }} { $this->worker = $worker; - var_dump("worker {$worker->id} start"); + dump("worker {$worker->id} start"); } public function onWorkerStop(Worker $worker) { - var_dump("worker {$worker->id} stop"); + dump("worker {$worker->id} stop"); } public function onConnect(TcpConnection $connection) { $this->connection = $connection; - var_dump("client connect to worker_id {$this->worker->id} successful, current connection_id is {$connection->id}"); + dump("client connect to worker_id {$this->worker->id} successful, current connection_id is {$connection->id}"); } public function onMessage(TcpConnection $connection, $data) { - var_dump($message = "the worker_id {$this->worker->id} of connection_id {$connection->id} receive message from client: " . $data); + dump($message = "the worker_id {$this->worker->id} of connection_id {$connection->id} receive message from client: " . $data); $connection->send($message); } @@ -40,6 +40,6 @@ class {{ class }} { $connection->close(); - var_dump("the worker_id {$this->worker->id} of connection_id {$connection->id} closed"); + dump("the worker_id {$this->worker->id} of connection_id {$connection->id} closed"); } } diff --git a/src/Commands/stubs/Events.stub b/src/Commands/stubs/Events.stub index 6480134..c73ac20 100644 --- a/src/Commands/stubs/Events.stub +++ b/src/Commands/stubs/Events.stub @@ -2,21 +2,146 @@ namespace {{ namespace }}; +use GatewayWorker\Lib\Gateway; +use GatewayWorker\BusinessWorker; + +/** + * @https://github.com/mouyong/laravel-octane-workerman/#websockets + * + * @link ws://localhost/ws + */ class {{ class }} { - public static function onWorkerStart($worker) + public static $whiteEvent = [ + 'pong', + 'login', + 'chat.send', + ]; + + public static function encode(array $message = []) + { + return json_encode($message, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT); + } + + public static function decode(string $message) + { + $data = json_decode($message, true) ?? []; + if (json_last_error()) { + info('json 解析错误 code: '.json_last_error() ." reason: ". json_last_error_msg(). ' message: '.$message); + + $data = []; + } + + if (!is_array($data)) { + $data = []; + } + + return $data; + } + + public static function sendToClient(string $client_id, string $message) + { + return Gateway::sendToClient($client_id, $message); + } + + public static function sendToGroup(string $group, string $message) + { + return Gateway::sendToGroup($group, $message); + } + + public static function sendToAll(string $message) + { + return Gateway::sendToAll($message); + } + + public static function onWorkerStart(BusinessWorker $worker) { create_laravel_application_for_worker($worker); + + dump(sprintf('%s #%s start', $worker->name, $worker->id)); + } + + public static function onConnect(string $client_id) + { + // 建立连接 + dump(sprintf('client_id #%s connect', $client_id)); } public static function onMessage() { - var_dump(config('app.debug')); - var_dump(func_get_args()); + $args = func_get_args(); + [$client_id, $message] = $args; + $data = Events::decode($message); + + // dump($_SERVER); + // dump($_SESSION); + // dump('debug mode enable? ' . config('app.debug')); + // dump(sprintf('client_id %s send to server message: %s', $client_id, $message)); + // dump('receive data: ' . var_export($data, true)); + + // Gateway::bindUid($client_id, $uid = uniqid()); + // Gateway::sendToUid($uid, 'you said: ' . $message); + + $eventType = $data['event_type'] ?? $data['type'] ?? null; + + $uid = Gateway::getUidByClientId($client_id); + + // 检验用户是否登录 + $user = $_SESSION['uid'] ?? null; + if (!in_array($eventType, Events::$whiteEvent)) { + if (!$user) { + return Gateway::sendToClient($client_id, downlink(Events::LOGIN_RESP, null, 403, 'Please login.')); + } + } + + switch ($eventType) { + // 维持客户端在线状态 + case 'pong': + $_SESSION['tick_time'] = date('Y-m-d H:i:s', time()); + break; + case 'login': + $token = data_get($data, 'data.token'); + if (!$token) { + return Gateway::sendToClient($client_id, downlink(Events::LOGIN_RESP, null, 500, 'Please login')); + } + + $_SESSION['userInfo'] = []; + $_SESSION['userInfo']['uid'] = uniqid(); + Gateway::updateSession($client_id, [ + 'userInfo' => [ + 'userName' => uniqid(), + ], + ]); + Gateway::bindUid($client_id, $_SESSION['userInfo']['uid']); + break; + case 'chat.send': + $to_user = data_get($data, 'data.to_user'); + $content = data_get($data, 'data.content'); + + if (!$to_user) { + return Gateway::sendToClient($client_id, downlink(Events::CHAT_SEND_RESP, null, 400, 'Please select chat_room')); + } + + if (!$content) { + return Gateway::sendToClient($client_id, downlink(Events::CHAT_SEND_RESP, null, 400, 'Please input message')); + } + + Gateway::sendToGroup($to_user, $content); + break; + default: + Gateway::sendToClient($client_id, '非法请求'); + break; + } + } + + public static function onClose(string $client_id) + { + // 清理数据 + dump(sprintf('client_id #%s disconnect', $client_id)); } - public static function onWorkerStop($connection) + public static function onWorkerStop(BusinessWorker $worker) { - $connection->close(); + dump(sprintf('%s #%s stop', $worker->name, $worker->id)); } } diff --git a/src/Commands/stubs/Sockets.stub b/src/Commands/stubs/Sockets.stub index 8887c20..e0fe2b8 100644 --- a/src/Commands/stubs/Sockets.stub +++ b/src/Commands/stubs/Sockets.stub @@ -2,13 +2,15 @@ namespace {{ namespace }}; +use GatewayWorker\Gateway; + class {{ class }} { - public static function onWorkerStart($connection) + public static function onWorkerStart(Gateway $worker) { } - public static function onWorkerStop($connection) + public static function onWorkerStop(Gateway $worker) { } @@ -20,4 +22,4 @@ class {{ class }} { $connection->close(); } -} \ No newline at end of file +}