{"id":20441749,"url":"https://github.com/mix-php/websocket","last_synced_at":"2025-10-09T01:16:18.011Z","repository":{"id":57017875,"uuid":"162685377","full_name":"mix-php/websocket","owner":"mix-php","description":"PHP WebSocket server and client based on Swoole coroutine / 基于 Swoole 协程的 PHP WebSocket 服务器与客户端","archived":false,"fork":false,"pushed_at":"2021-12-31T04:37:42.000Z","size":113,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-20T20:20:48.793Z","etag":null,"topics":["coroutine","mix","swoole","websocket"],"latest_commit_sha":null,"homepage":"https://openmix.org/mix-php","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}},"created_at":"2018-12-21T08:06:44.000Z","updated_at":"2024-12-16T14:57:00.000Z","dependencies_parsed_at":"2022-08-22T11:31:21.953Z","dependency_job_id":null,"html_url":"https://github.com/mix-php/websocket","commit_stats":null,"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"purl":"pkg:github/mix-php/websocket","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fwebsocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fwebsocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fwebsocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fwebsocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mix-php","download_url":"https://codeload.github.com/mix-php/websocket/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fwebsocket/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279000722,"owners_count":26082894,"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","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"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":["coroutine","mix","swoole","websocket"],"created_at":"2024-11-15T09:34:33.893Z","updated_at":"2025-10-09T01:16:17.985Z","avatar_url":"https://github.com/mix-php.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003e OpenMix 出品：[https://openmix.org](https://openmix.org/mix-php)\n\n# Mix WebSocket\n\nPHP WebSocket server and client based on Swoole coroutine\n\n基于 Swoole 协程的 PHP WebSocket 服务器与客户端\n\n## Overview\n\n该 WebSocket 支持处理服务器和客户端，服务器基于 Swoole 单进程协程 `Swoole\\Coroutine\\Http\\Server` 驱动，没有多进程那些复杂的作用域和生命周期概念，开发体验和 Golang 一致，简单又高效。\n\n**推荐搭配以下数据库使用 (支持协程和连接池)：**\n\n- https://github.com/mix-php/database\n- https://github.com/mix-php/redis\n\n**推荐搭配以下库处理 Subscribe：**\n\n- https://github.com/mix-php/redis-subscriber\n\n## 技术交流\n\n知乎：https://www.zhihu.com/people/onanying    \n官方QQ群：[284806582](https://shang.qq.com/wpa/qunwpa?idkey=b3a8618d3977cda4fed2363a666b081a31d89e3d31ab164497f53b72cf49968a)\n, [825122875](http://shang.qq.com/wpa/qunwpa?idkey=d2908b0c7095fc7ec63a2391fa4b39a8c5cb16952f6cfc3f2ce4c9726edeaf20)\n敲门暗号：ws\n\n## Installation\n\n- Swoole \u003e= 4.4.15: https://wiki.swoole.com/#/environment\n\n```\ncomposer require mix/websocket\n```\n\n## 服务器 Server\n\n在 [Mix Vega](https://github.com/mix-php/vega) 中使用 (只支持 Swoole 单进程协程)\n\n```php\n$upgrader = new Mix\\WebSocket\\Upgrader();\n\n$vega = new Mix\\Vega\\Engine();\n$vega-\u003ehandle('/websocket', function (Mix\\Vega\\Context $ctx) use ($upgrader) {\n    // 升级连接\n    $conn      = $upgrader-\u003eupgrade($ctx-\u003erequest, $ctx-\u003eresponse);\n\n    // 接收消息\n    $in        = $conn-\u003ereadMessage();\n    var_dump($in-\u003edata);\n    \n    // 发送消息\n    $out       = new Swoole\\WebSocket\\Frame();\n    $out-\u003edata = sprintf('hello, %s', $in-\u003edata);\n    $conn-\u003esend($out);\n    \n    $conn-\u003eclose();\n})-\u003emethods('GET');\n```\n\n在 Swoole 原生中使用 (只支持单进程协程)\n\n```php\nSwoole\\Coroutine\\run(function () {\n    $upgrader = new Mix\\WebSocket\\Upgrader();\n    $server = new Swoole\\Coroutine\\Http\\Server('0.0.0.0', 9502, false);\n    $server-\u003ehandle('/websocket', function (Swoole\\Http\\Request $request, Swoole\\Http\\Response $response) use ($upgrader) {\n        // 升级连接\n        $conn = $upgrader-\u003eupgradeRaw($request, $response);\n        \n        // ...\n    });\n    $server-\u003estart();\n});\n```\n\n获取当前连接数\n\n```php\n$total = $upgrader-\u003ecount();\n```\n\n关闭全部连接\n\n```php\n$upgrader-\u003ecloseAll();\n```\n\n## 客户端 Client\n\n可以连接任何 websocket v13 的服务器\n\n```php\n$cli   = Mix\\WebSocket\\Client('ws://127.0.0.1:9502/websocket');\n\n// 发送消息\n$out       = new Swoole\\WebSocket\\Frame();\n$out-\u003edata = 'xiaoming';\n$cli-\u003ewriteMessage($out);\n\n// 接收消息\n$in = $cli-\u003ereadMessage();\nvar_dump($in-\u003edata);\n\n$cli-\u003eclose();\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","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmix-php%2Fwebsocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fwebsocket/lists"}