{"id":20441723,"url":"https://github.com/mix-php/websocket-skeleton","last_synced_at":"2026-04-19T02:33:40.467Z","repository":{"id":57017820,"uuid":"386201734","full_name":"mix-php/websocket-skeleton","owner":"mix-php","description":"WebSocket development skeleton","archived":false,"fork":false,"pushed_at":"2023-04-19T02:02:04.000Z","size":46,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-20T23:49:23.394Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mix-php.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-15T07:23:03.000Z","updated_at":"2022-04-15T15:32:28.000Z","dependencies_parsed_at":"2024-11-15T09:36:29.237Z","dependency_job_id":null,"html_url":"https://github.com/mix-php/websocket-skeleton","commit_stats":{"total_commits":32,"total_committers":3,"mean_commits":"10.666666666666666","dds":0.125,"last_synced_commit":"355b88b95d06304979df2a7c2575d91d8bedf1c1"},"previous_names":[],"tags_count":29,"template":true,"template_full_name":null,"purl":"pkg:github/mix-php/websocket-skeleton","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fwebsocket-skeleton","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fwebsocket-skeleton/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fwebsocket-skeleton/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fwebsocket-skeleton/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mix-php","download_url":"https://codeload.github.com/mix-php/websocket-skeleton/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fwebsocket-skeleton/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31992012,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-18T20:23:30.271Z","status":"online","status_checked_at":"2026-04-19T02:00:07.110Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-11-15T09:34:26.135Z","updated_at":"2026-04-19T02:33:40.450Z","avatar_url":"https://github.com/mix-php.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WebSocket development skeleton\n\n帮助你快速搭建 WebSocket 项目骨架，并指导你如何使用该骨架的细节，骨架默认开启了 SQL、Redis 日志，压测前请先关闭 `.env` 的 `APP_DEBUG`\n\n## 安装\n\n\u003e 需要先安装 [Swoole](https://wiki.swoole.com/#/environment)\n\n- Swoole \u003e= 4.4.15: https://wiki.swoole.com/#/environment\n\n```\ncomposer create-project --prefer-dist mix/websocket-skeleton websocket\n```\n\n## 快速开始\n\n启动 Swoole 协程服务\n\n```\ncomposer run-script --timeout=0 swooleco:start\n```\n\n## 执行脚本\n\n- `composer run-script` 命令中的 `--timeout=0` 参数是防止 composer [执行超时](https://getcomposer.org/doc/06-config.md#process-timeout)\n- `composer.json` 定义了命令执行脚本，对应上面的执行命令\n\n```json\n\"scripts\": {\n    \"swooleco:start\": \"php bin/swooleco.php\",\n    \"cli:clearcache\": \"php bin/cli.php clearcache\"\n}\n```\n\n\n当然也可以直接下面这样启动，效果是一样的，但是 `scripts` 能帮你记录到底有哪些可用的命令，同时在IDE中调试更加方便。\n\n```\nphp bin/swooleco.php start\n```\n\n## 编写一个 WebSocket 服务\n\n首先修改根目录 `.env` 文件的数据库信息\n\n然后在 `routes/index.php` 定义一个新的路由\n\n```php\n$vega-\u003ehandle('/websocket', [new WebSocket(), 'index'])-\u003emethods('GET');\n```\n\n路由里使用了 `WebSocket` 控制器，我们需要创建他\n\n- 如何配置路由：[mix/vega](https://github.com/mix-php/vega#readme)\n- 如何使用 WebSocket 升级器：[mix/websocket](https://github.com/mix-php/websocket#readme)\n\n```php\n\u003c?php\n\nnamespace App\\Controller;\n\nuse App\\Container\\Upgrader;\nuse App\\Service\\Session;\nuse Mix\\Vega\\Context;\n\nclass WebSocket\n{\n\n    /**\n     * @param Context $ctx\n     */\n    public function index(Context $ctx)\n    {\n        $conn = Upgrader::instance()-\u003eupgrade($ctx-\u003erequest, $ctx-\u003eresponse);\n        $session = new Session($conn);\n        $session-\u003estart();\n    }\n\n}\n```\n\n控制器中使用了一个 `Session` 类来处理连接事务\n\n```php\n\u003c?php\n\nnamespace App\\Service;\n\nuse App\\Handler\\Hello;\nuse Mix\\WebSocket\\Connection;\nuse Swoole\\Coroutine\\Channel;\n\nclass Session\n{\n\n    /**\n     * @var Connection\n     */\n    protected $conn;\n\n    /**\n     * @var Channel\n     */\n    protected $writeChan;\n\n    /**\n     * Session constructor.\n     * @param Connection $conn\n     */\n    public function __construct(Connection $conn)\n    {\n        $this-\u003econn = $conn;\n        $this-\u003ewriteChan = new Channel(10);\n    }\n\n    /**\n     * @param string $data\n     */\n    public function send(string $data): void\n    {\n        $this-\u003ewriteChan-\u003epush($data);\n    }\n\n    public function start(): void\n    {\n        // 接收消息\n        go(function () {\n            while (true) {\n                $frame = $this-\u003econn-\u003erecv();\n                $message = $frame-\u003edata;\n\n                (new Hello($this))-\u003eindex($message);\n            }\n        });\n\n        // 发送消息\n        go(function () {\n            while (true) {\n                $data = $this-\u003ewriteChan-\u003epop();\n                if (!$data) {\n                    return;\n                }\n                $frame = new \\Swoole\\WebSocket\\Frame();\n                $frame-\u003edata = $data;\n                $frame-\u003eopcode = WEBSOCKET_OPCODE_TEXT; // or WEBSOCKET_OPCODE_BINARY\n                $this-\u003econn-\u003esend($frame);\n            }\n        });\n    }\n\n}\n```\n\n在接收消息处，使用了 `src/Handler/Hello.php` 处理器对当前发送的消息做逻辑处理，我们只需根据自己的需求增加新的处理器来处理不同消息即可。\n\n```\n(new Hello($this))-\u003eindex($message);\n```\n\n重新启动服务器后方可测试新开发的接口\n\n\u003e 实际开发中使用 PhpStorm 的 Run 功能，只需要点击一下重启按钮即可\n\n```\n// 查找进程 PID\nps -ef | grep swooleco\n\n// 通过 PID 停止进程\nkill PID\n\n// 重新启动进程\ncomposer run-script swooleco:start\n```\n\n使用测试工具测试\n\n- [WEBSOCKET 在线测试工具](http://www.easyswoole.com/wstool.html) `ws://127.0.0.1:9502/websocket`\n\n## 如何使用 WebSocket 客户端\n\n- [mix/websocket#客户端-client](https://github.com/mix-php/websocket#%E5%AE%A2%E6%88%B7%E7%AB%AF-client)\n\n## 使用容器中的对象\n\n容器采用了一个简单的单例模式，你可以修改为更加适合自己的方式。\n\n- 数据库：[mix/database](https://github.com/mix-php/database#readme)\n\n```\nDB::instance()\n```\n\n- Redis：[mix/redis](https://github.com/mix-php/redis#readme)\n\n```\nRDS::instance()\n```\n\n- 日志：[monolog/monolog](https://seldaek.github.io/monolog/doc/01-usage.html)\n\n```\nLogger::instance()\n```\n\n- 配置：[hassankhan/config](https://github.com/hassankhan/config#getting-values)\n\n```\nConfig::instance()\n```\n\n## 部署\n\n线上部署启动时，修改 `shell/server.sh` 脚本中的绝对路径和参数\n\n```\nphp=/usr/local/bin/php\nfile=/project/bin/swooleco.php\ncmd=start\nnumprocs=1\n```\n\n启动管理\n\n```\nsh shell/server.sh start\nsh shell/server.sh stop\nsh shell/server.sh restart\n```\n\n使用 `nginx` 或者 `SLB` 代理到服务器端口即可\n\n```\nlocation /websocket {\n    proxy_pass http://127.0.0.1:9502;\n    proxy_http_version 1.1;\n    proxy_set_header Upgrade $http_upgrade;\n    proxy_set_header Connection \"Upgrade\";\n}\n```\n\n## License\n\nApache License Version 2.0, http://www.apache.org/licenses/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fwebsocket-skeleton","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmix-php%2Fwebsocket-skeleton","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fwebsocket-skeleton/lists"}