PHPackages                             cyzonetech/think-swoole - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. cyzonetech/think-swoole

ActiveThink-extend[Framework](/categories/framework)

cyzonetech/think-swoole
=======================

Swoole extend for thinkphp5.1

v3.1.1.1(5y ago)07Apache-2.0PHPPHP &gt;7.1

Since Oct 5Pushed 5y agoCompare

[ Source](https://github.com/cyzonetech/think-swoole)[ Packagist](https://packagist.org/packages/cyzonetech/think-swoole)[ RSS](/packages/cyzonetech-think-swoole/feed)WikiDiscussions 2.0 Synced 2mo ago

READMEChangelog (3)Dependencies (7)Versions (25)Used By (0)

ThinkPHP 5.1 Swoole 扩展
======================

[](#thinkphp-51-swoole-扩展)

安装
--

[](#安装)

首先按照Swoole官网说明安装swoole扩展，然后使用

```
composer require topthink/think-swoole

```

安装swoole扩展。

使用方法
----

[](#使用方法)

### HttpServer

[](#httpserver)

直接在命令行下启动服务端。

```
php think swoole

```

启动完成后，会在0.0.0.0:9501启动一个HTTP Server，可以直接访问当前的应用。

swoole的参数可以在应用配置目录下的swoole.php里面配置（具体参考配置文件内容）。

如果需要使用守护进程方式运行，可以使用

```
php think swoole -d

```

或者在swoole.php文件中设置

```
'daemonize'	=>	true

```

注意：由于onWorkerStart运行的时候没有HTTP\_HOST，因此最好在应用配置文件中设置app\_host

支持的操作包括

```
php think swoole [start|stop|reload|restart]

```

### Server

[](#server)

可以支持直接启动一个Swoole server

```
php think swoole:server

```

会在0.0.0.0:9508启动一个Websocket服务。

如果需要自定义参数，可以在config/swoole\_server.php中进行配置，包括：

配置参数描述type服务类型host监听地址port监听端口mode运行模式sock\_typeSocket type并且支持swoole所有的参数。 也支持使用闭包方式定义相关事件回调。

```
return [
    // 扩展自身配置
    'host'         => '0.0.0.0', // 监听地址
    'port'         => 9501, // 监听端口
    'type'         => 'socket', // 服务类型 支持 socket http server
    'mode'         => SWOOLE_PROCESS,
    'sock_type'    => SWOOLE_SOCK_TCP,

    // 可以支持swoole的所有配置参数
    'daemonize'    => false,

    // 事件回调定义
    'onOpen'       => function ($server, $request) {
        echo "server: handshake success with fd{$request->fd}\n";
    },

    'onMessage'    => function ($server, $frame) {
        echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
        $server->push($frame->fd, "this is server");
    },

    'onRequest'    => function ($request, $response) {
        $response->end("Hello Swoole. #" . rand(1000, 9999) . "");
    },

    'onClose'      => function ($ser, $fd) {
        echo "client {$fd} closed\n";
    },
];

```

也可以使用自定义的服务类

```
