PHP code example of rotoos / think-plugs-workerman

1. Go to this page and download the library: Download rotoos/think-plugs-workerman library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rotoos / think-plugs-workerman example snippets


[
    // 服务监听地址
    'host'     => '127.0.0.1',
    // 服务监听端口
    'port'     => 2346,
    // 套接字上下文选项
    'context'  => [],
    // 高级自定义服务类
    'classes'  => '',
    // 消息请求回调处理
    'callable' => null,
    // 服务进程参数配置
    'worker'   => [
        'name'  => 'DeAdmin',
        'count' => 4,
    ],
    // 监控文件变更重载
    'files'    => [
        // 监控检测间隔(单位秒,零不监控)
        'time' => 3,
        // 文件监控目录(默认监控 app 目录)
        'path' => [],
    ],
    // 监控内存超限重载
    'memory'   => [
        // 监控检测间隔(单位秒,零不监控)
        'time'  => 60,
        // 限制内存大小(可选单位有 G M K )
        'limit' => '1G'
    ],
    // 自定义服务配置(可选)
    'customs'  => [
        // 自定义 text 服务
        'text' => [
            // 进程类型(Workerman|Gateway|Business)
            'type'    => 'workerman',
            // 监听地址(<协议>://<地址>:<端口>)
            'listen'  => 'text://0.0.0.0:8686',
            // 高级自定义服务类
            'classes' => '',
            // 套接字上下文选项
            'context' => [],
            // 服务进程参数配置
            'worker'  => [
                //'name' => 'TextTest',
                // onWorkerStart => [class,method]
                // onWorkerReload => [class,method]
                // onConnect => [class,method]
                // onBufferFull => [class,method]
                // onBufferDrain => [class,method]
                // onError => [class,method]
                // 设置连接的 onMessage 回调
                'onMessage' => function ($connection, $data) {
                    dump($data);
                    $connection->send("hello world");
                }
            ]
        ],
        // 自定义 WebSocket 服务
        'websocket' => [
            // 进程类型(Workerman|Gateway|Business) 
            'type'    => 'Gateway',
            // 监听地址(<协议>://<地址>:<端口>)
            'listen'  => 'websocket://0.0.0.0:8688',
            // 高级自定义服务类
            'classes' => '',
            // 套接字上下文选项
            'context' => [],
            // 服务进程参数配置
            'worker'  => [
                //'name' => 'WebsocketTest',
                // onWorkerStart => [class,method]
                // onWorkerReload => [class,method]
                // onConnect => [class,method]
                // onBufferFull => [class,method]
                // onBufferDrain => [class,method]
                // onError => [class,method]
                // 设置连接的 onMessage 回调
                'onMessage' => function ($connection, $data) {
                    //// $connection->worker->connections 为全部连接
                    // foreach($connection->worker->connections as $con)
                    // {
                    //    $con->send($data);
                    // }
                    $connection->send("hello world");
                }
            ]
        ],
    ],
];
shell
php think xadmin:worker [start|stop|reload|restart|status|-d]

# 以上所有操作效果与 Workerman 官方操作一致,详情请阅读对应文档。