{"id":13755517,"url":"https://github.com/fastdlabs/swoole","last_synced_at":"2026-01-11T16:51:19.724Z","repository":{"id":62503623,"uuid":"38911741","full_name":"fastdlabs/swoole","owner":"fastdlabs","description":"FastD Swoole 基础组件","archived":false,"fork":false,"pushed_at":"2023-11-13T10:15:18.000Z","size":597,"stargazers_count":75,"open_issues_count":0,"forks_count":16,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-09T20:44:52.051Z","etag":null,"topics":["fastd","http","pecl","php","server","swoole","swoole-extension","swoole-http","swoole-tcp","swoole-udp","swoole-websocket"],"latest_commit_sha":null,"homepage":"","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/fastdlabs.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-07-11T03:10:26.000Z","updated_at":"2024-12-16T07:19:20.000Z","dependencies_parsed_at":"2023-11-13T11:30:37.744Z","dependency_job_id":"b1991320-80d0-41ba-8c0a-f9e4f3b381e9","html_url":"https://github.com/fastdlabs/swoole","commit_stats":{"total_commits":377,"total_committers":6,"mean_commits":"62.833333333333336","dds":0.5039787798408488,"last_synced_commit":"ffddc58ccae454f3ad2e52df2c38ed6f17f9c519"},"previous_names":["janhuang/swoole"],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastdlabs%2Fswoole","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastdlabs%2Fswoole/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastdlabs%2Fswoole/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastdlabs%2Fswoole/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fastdlabs","download_url":"https://codeload.github.com/fastdlabs/swoole/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253349979,"owners_count":21894813,"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":["fastd","http","pecl","php","server","swoole","swoole-extension","swoole-http","swoole-tcp","swoole-udp","swoole-websocket"],"created_at":"2024-08-03T10:00:55.684Z","updated_at":"2026-01-11T16:51:19.717Z","avatar_url":"https://github.com/fastdlabs.png","language":"PHP","funding_links":[],"categories":["网络编程"],"sub_categories":[],"readme":"# FastD Swoole\n\n[![Build Status](https://travis-ci.org/fastdlabs/swoole.svg?branch=master)](https://travis-ci.org/fastdlabs/swoole)\n[![PHP Require Version](https://img.shields.io/badge/php-%3E%3D5.6-8892BF.svg)](https://secure.php.net/)\n[![Swoole Require Version](https://img.shields.io/badge/swoole-%3E%3D1.9.6-8892BF.svg)](http://www.swoole.com/)\n[![Latest Stable Version](https://poser.pugx.org/fastd/swoole/v/stable)](https://packagist.org/packages/fastd/swoole)\n[![Total Downloads](https://poser.pugx.org/fastd/swoole/downloads)](https://packagist.org/packages/fastd/swoole) \n[![Latest Unstable Version](https://poser.pugx.org/fastd/swoole/v/unstable)](https://packagist.org/packages/fastd/swoole) \n[![License](https://poser.pugx.org/fastd/swoole/license)](https://packagist.org/packages/fastd/swoole)\n\n高性能网络服务组件. 提供底层服务封装, 基础管理及客户端调用功能. 使用 `composer` 进行管理, 可在此基础上进行封装整合.\n\n## 环境要求\n\n* Linux (不考虑windows)\n* PHP \u003e= 5.6\n* swoole \u003e= 1.9.6\n\n源码地址: [swoole](https://github.com/swoole/swoole-src)\n\npecl 安装\n\n```shell\npecl install swoole\n```\n\n### 可选扩展\n\n**如果 PHP \u003e= 7.0 的安装 2.0 版本.**\n\n源码地址: [inotify](http://pecl.php.net/package/inotify)\n\npecl 安装\n\n```shell\npecl install inotify\n```\n\n### 安装\n\n```\ncomposer require fastd/swoole\n```\n\n## 文档\n\n[中文文档](docs/zh_CN/readme.md)\n\n## 使用\n\n服务继承 `FastD\\Swoole\\Server`, 实现 `doWork` 方法, 服务器在接收信息 `onReceive` 回调中会调用 `doWork` 方法, `doWork` 方法接受一个封装好的请求对象。\n\n具体逻辑在 `doWork` 方法中实现, `doWork` 方法中返回响应客户端的数据, 格式为: **字符串**\n\nSwoole 配置通过实现 `configure` 方法进行配置，具体配置参数请参考: [Swoole 配置选项](http://wiki.swoole.com/wiki/page/274.html)\n\n#### TCP Server\n\n```php\nclass DemoServer extends \\FastD\\Swoole\\Server\\Tcp\n{\n    public function doWork(swoole_server $server, $fd, $data, $from_id)\n    {\n        echo $data . PHP_EOL;\n        return 'hello tcp';\n    }\n}\n\nDemoServer::createServer('tcp swoole', 'tcp://0.0.0.0:9527')-\u003estart();\n```\n\n#### UDP Server\n\n```php\nclass DemoServer extends \\FastD\\Swoole\\Server\\Udp\n{\n    public function doPacket(swoole_server $server, $data, $client_info)\n    {\n        echo $data . PHP_EOL;\n        return 'hello tcp';\n    }\n}\n\nDemoServer::createServer('udp swoole', 'udp://127.0.0.1:9527')-\u003estart;\n```\n\n#### HTTP Server\n\n同理, `Http` 服务器扩展 `Server` 类, 实现 `doRequest` 方法,实现具体逻辑。\n\n```php\nclass Http extends \\FastD\\Swoole\\Server\\Http\n{\n    public function doRequest(ServerRequest $serverRequest)\n    {\n        return new JsonResponse([\n            'msg' =\u003e 'hello world',\n        ]);\n    }\n}\n\nHttp::createServer('http', 'http://0.0.0.0:9527')-\u003estart();\n```\n\n目前 Http 服务支持 Session 存储，而 Session 存储是基于浏览器 cookie，或者可以自定义实现存储方式。\n\n目前由 [FastD/Session](https://github.com/JanHuang/http) 提供 session 支持以及 swoole_http_request 对象解释。\n\n#### WebSocket Server\n\n```php\nclass WebSocket extends \\FastD\\Swoole\\Server\\WebSocket\n{\n    public function doOpen(swoole_websocket_server $server, swoole_http_request $request)\n    {\n        echo \"server: handshake success with fd{$request-\u003efd}\\n\";\n    }\n\n    public function doMessage(swoole_server $server, swoole_websocket_frame $frame)\n    {\n        echo \"receive from {$frame-\u003efd}:{$frame-\u003edata},opcode:{$frame-\u003eopcode},fin:{$frame-\u003efinish}\\n\";\n        $server-\u003epush($frame-\u003efd, \"this is server\");\n    }\n}\n\nWebSocket::createServer('ws', 'ws://0.0.0.0:9527')-\u003estart();\n```\n\n#### 多端口支持\n\n```php\nclass Server extends \\FastD\\Swoole\\Server\\Tcp\n{\n    public function doWork(swoole_server $server, $fd, $data, $from_id)\n    {\n        return 'hello server1';\n    }\n}\n\nclass Server2 extends \\FastD\\Swoole\\Server\\Tcp\n{\n    public function doWork(swoole_server $server, $fd, $data, $from_id)\n    {\n        return 'hello server2';\n    }\n}\n\n$server = new Server('tcp server', 'tcp://127.0.0.1:9527');\n\n$server-\u003elisten(new Server2('tcp server2', 'tcp://127.0.0.1:9528'));\n\n$server-\u003estart();\n```\n\n#### 服务管理\n\n```php\nclass DemoServer extends \\FastD\\Swoole\\Server\\Tcp\n{\n    public function doWork(swoole_server $server, $fd, $data, $from_id)\n    {\n        echo $data . PHP_EOL;\n        return 'hello tcp';\n    }\n}\n\n$server = DemoServer::createServer('tcp swoole', 'tcp://0.0.0.0:9527');\n\n$argv = $_SERVER['argv'];\n\n$argv[1] = isset($argv[1]) ? $argv[1] : 'status';\n\nswitch ($argv[1]) {\n    case 'start':\n        $server-\u003estart();\n        break;\n    case 'stop':\n        $server-\u003eshutdown();\n        break;\n    case 'reload':\n        $server-\u003ereload();\n        break;\n    case 'status':\n    default:\n        $server-\u003estatus();\n}\n```\n\n#### File Listener\n\n因为 swoole 是常驻内存，程序通过首次启动就自动预载到内存当中，所以每次修改都需要重启服务。\n\n所以这里提供监听文件变化来到自动重启服务(建议开发环境中使用)\n\n```php\n\nclass DemoServer extends \\FastD\\Swoole\\Server\\Tcp\n{\n    public function doWork(swoole_server $server, $fd, $data, $from_id)\n    {\n        return 'hello tcp';\n    }\n}\n\n$server = new DemoServer('watch server', 'tcp://0.0.0.0:9527');\n// $server = DemoServer::createServer('watch server', 'tcp://0.0.0.0:9527');\n$server-\u003ewatch([__DIR__ . '/listen_files']);\n```\n\n#### Sync Client\n\nClient 通过 resolve 执行，通过不同的方法设置不同的回调，同步、异步均使用通用的方法。\n\n```php\n$client = new \\FastD\\Swoole\\Client\\Sync\\SyncClient('tcp://11.11.11.11:9527');\n\n$client\n    -\u003econnect(function ($client) {\n        $client-\u003esend('hello world');\n    })\n    -\u003ereceive(function ($client, $data) {\n        echo $data . PHP_EOL;\n        $client-\u003eclose();\n    })\n    -\u003eresolve()\n;\n```\n\n#### Async Client\n\n```php\n$client = new \\FastD\\Swoole\\Client\\Async\\AsyncClient('tcp://11.11.11.11:9527');\n\n$client\n    -\u003econnect(function ($client) {\n        $client-\u003esend('hello world');\n    })\n    -\u003ereceive(function ($client, $data) {\n        echo $data . PHP_EOL;\n    })\n    -\u003eerror(function ($client) {\n        print_r($client);\n    })\n    -\u003eclose(function ($client) {})\n    -\u003eresolve()\n;\n```\n\n#### Process\n\n```php\n$process = new Process('single', function () {\n    timer_tick(1000, function ($id) {\n        static $index = 0;\n        $index++;\n        echo $index . PHP_EOL;\n        if ($index === 10) {\n            timer_clear($id);\n        }\n    });\n});\n\n$process-\u003estart();\n\n$process-\u003ewait(function ($ret) {\n    echo 'PID: ' . $ret['pid'];\n});\n```\n\n#### Multi Process\n\n```php\n$process = new Process('multi', function () {\n    timer_tick(1000, function ($id) {\n        static $index = 0;\n        $index++;\n        echo $index . PHP_EOL;\n        if ($index === 10) {\n            timer_clear($id);\n        }\n    });\n});\n\n$process-\u003efork(5);\n\n$process-\u003ewait(function ($ret) {\n    echo 'PID: ' . $ret['pid'] . PHP_EOL;\n});\n```\n\n#### Queue\n\n```php\n$queue = new \\FastD\\Swoole\\Queue('queue', function ($worker) {\n    while (true) {\n        $recv = $worker-\u003epop();\n        echo \"From Master: $recv\\n\";\n    }\n});\n\n$queue-\u003estart();\n\nwhile (true) {\n    $queue-\u003epush('hello');\n    sleep(1);\n}\n\n\n$queue-\u003ewait(function ($ret) {\n    echo 'PID: ' . $ret['pid'];\n});\n```\n\n### 贡献\n\n非常欢迎感兴趣，愿意参与其中，共同打造更好PHP生态，Swoole生态的开发者。\n\n如果你乐于此，却又不知如何开始，可以试试下面这些事情：\n\n* 在你的系统中使用，将遇到的问题 [反馈](https://github.com/JanHuang/fastD/issues)。\n* 有更好的建议？欢迎联系 [bboyjanhuang@gmail.com](mailto:bboyjanhuang@gmail.com) 或 [新浪微博:编码侠](http://weibo.com/ecbboyjan)。\n\n### 联系\n\n如果你在使用中遇到问题，请联系: [bboyjanhuang@gmail.com](mailto:bboyjanhuang@gmail.com). 微博: [编码侠](http://weibo.com/ecbboyjan)\n\n## License MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastdlabs%2Fswoole","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffastdlabs%2Fswoole","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastdlabs%2Fswoole/lists"}