{"id":15374312,"url":"https://github.com/yurunsoft/workerman-gateway-sdk","last_synced_at":"2025-04-15T12:31:55.352Z","repository":{"id":57088665,"uuid":"358178573","full_name":"Yurunsoft/workerman-gateway-sdk","owner":"Yurunsoft","description":"一个支持在 Swoole 或其它非 Workerman 环境，开发 Gateway Worker 的组件。","archived":false,"fork":false,"pushed_at":"2021-06-24T08:28:11.000Z","size":26,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T08:09:05.024Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Yurunsoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-04-15T08:08:54.000Z","updated_at":"2024-12-16T03:12:56.000Z","dependencies_parsed_at":"2022-08-20T15:31:12.185Z","dependency_job_id":null,"html_url":"https://github.com/Yurunsoft/workerman-gateway-sdk","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2Fworkerman-gateway-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2Fworkerman-gateway-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2Fworkerman-gateway-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2Fworkerman-gateway-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Yurunsoft","download_url":"https://codeload.github.com/Yurunsoft/workerman-gateway-sdk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249072315,"owners_count":21208169,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T13:58:19.500Z","updated_at":"2025-04-15T12:31:55.063Z","avatar_url":"https://github.com/Yurunsoft.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Workerman Gateway SDK\n\n一个支持在 Swoole 或其它非 Workerman 环境，开发 Gateway Worker 的组件。\n\n支持用 Workerman Gateway 做网关，Swoole 编写业务代码。\n\n## 安装\n\n`composer require yurunsoft/workerman-gateway-sdk`\n\n## Swoole Demo\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nuse GatewayWorker\\Lib\\Context;\nuse GatewayWorker\\Lib\\Gateway;\nuse GatewayWorker\\Protocols\\GatewayProtocol;\nuse Swoole\\Coroutine;\nuse Swoole\\Coroutine\\Channel;\nuse function Swoole\\Coroutine\\parallel;\nuse Workerman\\Gateway\\Config\\GatewayWorkerConfig;\nuse Workerman\\Gateway\\Gateway\\Contract\\IGatewayClient;\nuse Workerman\\Gateway\\Gateway\\GatewayWorkerClient;\n\nrequire dirname(__DIR__) . '/vendor/autoload.php';\n\nCo\\run(function () {\n    $channel = new Channel(1024);\n\n    Coroutine::create(function () use ($channel) {\n        // 通过 Channel 实现单进程多协程任务处理\n        parallel(swoole_cpu_num(), function () use ($channel) {\n            while (true)\n            {\n                $result = $channel-\u003epop();\n                if (false === $result)\n                {\n                    break;\n                }\n                switch ($result['type'])\n                {\n                    case 'onException':\n                        /** @var Throwable $th */\n                        ['th' =\u003e $th] = $result['data'];\n                        // 异常处理\n                        var_dump($th-\u003egetMessage(), $th-\u003egetTraceAsString());\n                        break;\n                    case 'onGatewayMessage':\n                        /** @var IGatewayClient $client */\n                        ['client' =\u003e $client, 'message' =\u003e $message] = $result['data'];\n                        var_dump($message);\n                        $clientId = Context::addressToClientId($message['local_ip'], $message['local_port'], $message['connection_id']);\n                        switch ($message['cmd']) {\n                            case GatewayProtocol::CMD_ON_CONNECT:\n                                // 连接\n                                var_dump('connect:' . $clientId);\n                                break;\n                            case GatewayProtocol::CMD_ON_MESSAGE:\n                                var_dump('message:' . $clientId, 'body:' . $message['body']);\n                                $data = json_decode($message['body'], true);\n                                switch ($data['action'] ?? '')\n                                {\n                                    case 'send':\n                                        // {\"action\":\"send\", \"content\":\"test content\"}\n                                        // 广播给所有用户\n                                        Gateway::sendToAll(json_encode([\n                                            'action'  =\u003e 'receive',\n                                            'content' =\u003e $data['content'] ?? '',\n                                        ]));\n                                        break;\n                                }\n                                break;\n                            case GatewayProtocol::CMD_ON_CLOSE:\n                                var_dump('close:' . $clientId);\n                                break;\n                            case GatewayProtocol::CMD_ON_WEBSOCKET_CONNECT:\n                                var_dump('websocket connect:' . $clientId, 'body:', $message['body']);\n                                break;\n                        }\n                        break;\n                }\n            }\n        });\n    });\n\n    $config = new GatewayWorkerConfig();\n    $config-\u003esetRegisterAddress('127.0.0.1:1238');\n\n    // Gateway Client 配置\n    Gateway::$registerAddress = $config-\u003egetRegisterAddress();\n\n    $workerKey = getmypid() . '-' . Coroutine::getuid();\n    // Gateway Worker\n    $client = new GatewayWorkerClient($workerKey, $config);\n    // 异常处理\n    $client-\u003eonException = function (Throwable $th) use ($channel) {\n        $channel-\u003epush([\n            'type' =\u003e 'onException',\n            'data' =\u003e [\n                'th' =\u003e $th,\n            ],\n        ]);\n    };\n    // 网关消息\n    $client-\u003eonGatewayMessage = function (IGatewayClient $client, array $message) use ($channel) {\n        $channel-\u003epush([\n            'type' =\u003e 'onGatewayMessage',\n            'data' =\u003e [\n                'client'  =\u003e $client,\n                'message' =\u003e $message,\n            ],\n        ]);\n    };\n    $client-\u003erun();\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyurunsoft%2Fworkerman-gateway-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyurunsoft%2Fworkerman-gateway-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyurunsoft%2Fworkerman-gateway-sdk/lists"}