{"id":13635745,"url":"https://github.com/wp-breeder/swoft-socket","last_synced_at":"2025-04-19T04:31:25.195Z","repository":{"id":57082016,"uuid":"156347156","full_name":"wp-breeder/swoft-socket","owner":"wp-breeder","description":"socket for Swoft  v1.0","archived":true,"fork":false,"pushed_at":"2018-12-27T09:57:35.000Z","size":28,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-04T20:17:41.129Z","etag":null,"topics":["php","swoft","swoole","tcp","udp"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wp-breeder.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":"2018-11-06T07:59:24.000Z","updated_at":"2023-01-28T15:40:25.000Z","dependencies_parsed_at":"2022-08-24T14:58:18.572Z","dependency_job_id":null,"html_url":"https://github.com/wp-breeder/swoft-socket","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wp-breeder%2Fswoft-socket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wp-breeder%2Fswoft-socket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wp-breeder%2Fswoft-socket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wp-breeder%2Fswoft-socket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wp-breeder","download_url":"https://codeload.github.com/wp-breeder/swoft-socket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249606341,"owners_count":21298851,"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":["php","swoft","swoole","tcp","udp"],"created_at":"2024-08-02T00:00:51.058Z","updated_at":"2025-04-19T04:31:24.894Z","avatar_url":"https://github.com/wp-breeder.png","language":"PHP","funding_links":[],"categories":["Popular Packages"],"sub_categories":[],"readme":"## swoft-socket\r\n\r\n### 说明\r\n\r\n  基于 **[swoft](https://github.com/swoft-cloud/swoft)** 的 `socket` 组件，支持 `tcp/udp` 多端口监听，详细内容参考[swoole wiki](https://wiki.swoole.com/wiki/page/161.html)。\r\n\r\n\r\n### 安装\r\n\r\n1.  composer command\r\n\r\n```shell\r\n    composer require wp-breeder/swoft-socket    \r\n```\r\n\r\n### 快速开始\r\n\r\n1. 在 `/project-to-path/config/properties/app` 中添加配置:\r\n\r\n```php\r\n    // 配置启动扫描\r\n    'bootScan' =\u003e [\r\n        // code...\r\n        // 把 sockerListener 注解的目录添加到启动扫描\r\n        'App\\Listener'\r\n    ],\r\n    // 指定扫描组件\r\n    'components' =\u003e [\r\n            'custom' =\u003e [\r\n                'Swoft\\\\Socket\\\\'\r\n            ],\r\n        ],\r\n```\r\n\r\n2. 在 `/project-to-path/config/server` 中添加配置:\r\n\r\n```php\r\n    //for socket component\r\n    'server' =\u003e [\r\n            //code ...\r\n            // socketable 配置是否开启socket监听\r\n            'socketable' =\u003e env('SOCKETABLE', false),\r\n           \r\n        ],\r\n    \r\n    // for socket component test\r\n        'socket' =\u003e [\r\n            [\r\n                'host' =\u003e env('SOCKET_TCP_HOST', '0.0.0.0'),\r\n                'port' =\u003e env('SOCKET_TCP_PORT', 8010),\r\n                'mode' =\u003e env('SOCKET_TCP_MODE', SWOOLE_PROCESS),\r\n                'type' =\u003e env('SOCKET_TCP_TYPE', SWOOLE_SOCK_TCP),\r\n                //server 名称,必须设置，如果未设置则不监听(用于区分不同server 设置不同的回调，如果重复，则会覆盖)\r\n                //如果是单独开启，第一个不设置name则会报错\r\n                'name' =\u003e 'tcp'\r\n            ],\r\n            [\r\n                'host' =\u003e env('SOCKET_UDP_HOST', '0.0.0.0'),\r\n                'port' =\u003e env('SOCKET_UDP_PORT', 8011),\r\n                'mode' =\u003e env('SOCKET_UDP_MODE', SWOOLE_PROCESS),\r\n                'type' =\u003e env('SOCKET_UDP_TYPE', SWOOLE_SOCK_UDP),\r\n                'name' =\u003e 'udp'\r\n            ],\r\n        ],\r\n```\r\n\r\n### 用法\r\n\r\n1. 与 `rpc`、`http` 混合使用\r\n\r\n\u003e新增注解 `@SocketListener` , 使用方法大致与 `@ServerListener` 一致，不同是需要指定该监听器监听的端口。\r\n  \r\n```php\r\n    /**\r\n     *   监听多个事件\r\n     * @SocketListener({\r\n     *     SwooleEvent::ON_CONNECT,\r\n     *     SwooleEvent::ON_RECEIVE,\r\n     *     SwooleEvent::ON_CLOSE,\r\n     * },\r\n     *   监听端口的名称，与socket配置文件中的name对应(如重复则最后一个生效，不存在则监听不生效)\r\n     *     name=\"tcp\"\r\n     * )\r\n```\r\n\r\n2. 单独开启，与 `rpc` 、 `http` 、 `ws` 一致。(若监听多个，默认第一个配置为master server, 其他配置单独监听指定端口)\r\n\r\n```shell\r\n    php bin/swoft socket:start\r\n```\r\n\r\n\r\n3. 使用示例：\r\n\r\n```php\r\nnamespace SwoftTest\\Socket\\Testing\\Listeners;\r\n\r\nuse Swoft\\Bootstrap\\Listeners\\Interfaces\\CloseInterface;\r\nuse Swoft\\Bootstrap\\Listeners\\Interfaces\\ConnectInterface;\r\nuse Swoft\\Bootstrap\\Listeners\\Interfaces\\ReceiveInterface;\r\nuse Swoft\\Socket\\Bean\\Annotation\\SocketListener;\r\nuse Swoft\\Socket\\Event\\SwooleEvent;\r\nuse Swoole\\Server;\r\n\r\n/**\r\n * tcp 监听 demo\r\n * Class SocketListenerTest\r\n * @package SwoftTest\\Socket\\Testing\\Listeners\r\n * @SocketListener({\r\n *     SwooleEvent::ON_CONNECT,\r\n *     SwooleEvent::ON_RECEIVE,\r\n *     SwooleEvent::ON_CLOSE,\r\n * },\r\n *     name=\"tcp\"\r\n * )\r\n */\r\nclass TcpListener implements ReceiveInterface, CloseInterface, ConnectInterface\r\n{\r\n    public function onConnect(Server $server, int $fd, int $reactorId)\r\n    {\r\n        echo \"tcp connect\" . PHP_EOL;\r\n    }\r\n\r\n    public function onReceive(Server $server, int $fd, int $reactorId, string $data)\r\n    {\r\n        echo \"tcp receive data: \" . $data . PHP_EOL;\r\n    }\r\n\r\n    public function onClose(Server $server, int $fd, int $reactorId)\r\n    {\r\n        echo \"tcp close\" . PHP_EOL;\r\n    }\r\n\r\n}\r\n\r\n```\r\n\r\n```php\r\nnamespace SwoftTest\\Socket\\Testing\\Listeners;\r\n\r\nuse Swoft\\Socket\\Event\\SwooleEvent;\r\nuse Swoft\\Bootstrap\\Listeners\\Interfaces\\PacketInterface;\r\nuse Swoft\\Socket\\Bean\\Annotation\\SocketListener;\r\nuse Swoole\\Server;\r\n\r\n/**\r\n * udp 监听 demo\r\n * Class PacketListener\r\n * @package SwoftTest\\Socket\\Testing\\Listeners\r\n * @SocketListener(event={SwooleEvent::ON_PACKET}, name=\"udp\")\r\n */\r\nclass PacketListener implements PacketInterface\r\n{\r\n    public function onPacket(Server $server, string $data, array $clientInfo)\r\n    {\r\n        echo \"udp receive data: \" . $data;\r\n    }\r\n}\r\n```\r\n\r\n### LICENSE\r\n\r\nThe Component is open-sourced software licensed under the [Apache license](https://github.com/wp-breeder/swoft-socket/blob/master/LICENSE).\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwp-breeder%2Fswoft-socket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwp-breeder%2Fswoft-socket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwp-breeder%2Fswoft-socket/lists"}