PHP code example of imiphp / project-websocket

1. Go to this page and download the library: Download imiphp/project-websocket 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/ */

    

imiphp / project-websocket example snippets


[
    // 主服务器配置
    'mainServer'    =>    'swoole' === $mode ? [
        'namespace' =>  'ImiApp\WebSocketServer',
        'type'      =>  Imi\Swoole\Server\Type::WEBSOCKET,
        'host'      =>  '0.0.0.0',
        'port'      =>  8081,
        'mode'      =>  SWOOLE_BASE,
        'configs'   =>    [
            // 'worker_num'        =>  8,
            // 'task_worker_num'   =>  16,
        ],
        'beans' => [
            'ServerUtil' => \Imi\Swoole\Server\Util\LocalServerUtil::class,
        ],
    ] : [],

    // 子服务器(端口监听)配置
    'subServers'        =>    'swoole' === $mode ? [
        // 'SubServerName'   =>  [
        //     'namespace'    =>    'ImiApp\XXXServer',
        //     'type'        =>    Imi\Server\Type::HTTP,
        //     'host'        =>    '0.0.0.0',
        //     'port'        =>    13005,
        // ]
    ] : [],

    // Workerman 服务器配置
    'workermanServer' => 'workerman' === $mode ? [
        'http' => [
            'namespace' => 'ImiApp\WebSocketServer',
            'type'      => Imi\Workerman\Server\Type::HTTP,
            'host'      => '0.0.0.0',
            'port'      => 8080,
            'configs'   => [
                'count' => 1,
            ],
            'beans' => [
                'ServerUtil' => \Imi\Workerman\Server\Util\LocalServerUtil::class,
            ],
        ],
    ] : [],
]

[
    'beans' => [
        'ConnectionContextStore'   =>  [
            'handlerClass'  =>  \Imi\Server\ConnectionContext\StoreHandler\Local::class,
        ],
        'ConnectionContextLocal'    =>    [
            'lockId'    =>  null, // 非必设,可以用锁来防止数据错乱问题
        ],
        'ServerGroup' => [
            'groupHandler' => 'GroupLocal',
        ],
    ],
]

[
    
    // 主服务器配置
    'mainServer'    =>    'swoole' === $mode ? [
        'namespace' =>  'ImiApp\WebSocketServer',
        'type'      =>  Imi\Swoole\Server\Type::WEBSOCKET,
        // 'type'      => \Imi\WorkermanGateway\Swoole\Server\Type::BUSINESS_WEBSOCKET, // workerman gateway 模式 Worker
        'host'      =>  '0.0.0.0',
        'port'      =>  8081,
        'mode'      =>  SWOOLE_BASE,
        'configs'   =>    [
            // 'worker_num'        =>  8,
            // 'task_worker_num'   =>  16,
        ],
        'beans' => [
            'ServerUtil' => 'RedisServerUtil',
        ],
    ] : [],
]

[
    'beans' => [
        // Redis 模式
        'ConnectionContextStore'   =>  [
            'handlerClass'  =>  \Imi\Server\ConnectionContext\StoreHandler\Redis::class,
        ],
        'ConnectionContextRedis'    =>    [
            'redisPool'    => 'redis', // Redis 连接池名称
            'redisDb'      => 0, // redis中第几个库
            'key'          => 'imi:connect_context', // 键
            'heartbeatTimespan' => 5, // 心跳时间,单位:秒
            'heartbeatTtl' => 8, // 心跳数据过期时间,单位:秒
            'dataEncode'=>  'serialize', // 数据写入前编码回调
            'dataDecode'=>  'unserialize', // 数据读出后处理回调
            'lockId'    =>  null, // 非必设,可以用锁来防止数据错乱问题
        ],
        'ServerGroup' => [
            'groupHandler' => 'GroupRedis',
        ],
    ],
]

[
    // Workerman 服务器配置
    'workermanServer' => 'workerman' === $mode ? [
        'http' => [
            'namespace' => 'ImiApp\WebSocketServer',
            'type'      => Imi\Workerman\Server\Type::HTTP,
            'host'      => '0.0.0.0',
            'port'      => 8080,
            'configs'   => [
                'count' => 1,
            ],
            'beans' => [
                'ServerUtil' => 'ChannelServerUtil',
            ],
        ],
        // Workerman Gateway 模式请注释 websocket
        'websocket' => [
            'namespace'   => 'ImiApp\WebSocketServer',
            'type'        => Imi\Workerman\Server\Type::WEBSOCKET,
            'host'        => '0.0.0.0',
            'port'        => 8081,
            'shareWorker' => 'http',
            'beans' => [
                'ServerUtil' => 'ChannelServerUtil',
            ],
        ],
        'channel' => [
            'namespace'   => '',
            'type'        => Imi\Workerman\Server\Type::CHANNEL,
            'host'        => '127.0.0.1',
            'port'        => 13005,
            'configs'     => [
            ],
        ],
    ] : [],
    'workerman' => [
        // 多进程通讯组件配置
        'channel' => [
            'host' => '127.0.0.1',
            'port' => 13005,
        ],
    ],
]

[
    'beans' => [
        'ConnectionContextStore'   => [
            'handlerClass'  => 'ConnectionContextLocal',
            'ttl'           => 600,
        ],
    ],
]

[
    // 主服务器配置
    'mainServer'    =>    'swoole' === $mode ? [
        'namespace' =>  'ImiApp\WebSocketServer',
        'type'      => \Imi\WorkermanGateway\Swoole\Server\Type::BUSINESS_WEBSOCKET, // workerman gateway 模式 Worker
        'host'      =>  '0.0.0.0',
        'port'      =>  8081,
        'mode'      =>  SWOOLE_BASE,
        'configs'   =>    [
            // 'worker_num'        =>  8,
            // 'task_worker_num'   =>  16,
        ],
        'beans' => [
            'ServerUtil' => 'SwooleGatewayServerUtil',
        ],
        // workerman gateway 模式
        'workermanGateway' => [
            'registerAddress'      => '127.0.0.1:13004',
            'worker_coroutine_num' => swoole_cpu_num(),
            'channel'              => [
                'size' => 1024,
            ],
        ],
    ] : [],

    // 子服务器(端口监听)配置
    'subServers'        =>    'swoole' === $mode ? [
        // 'SubServerName'   =>  [
        //     'namespace'    =>    'ImiApp\XXXServer',
        //     'type'        =>    Imi\Server\Type::HTTP,
        //     'host'        =>    '0.0.0.0',
        //     'port'        =>    13005,
        // ]
    ] : [],

    // Workerman 服务器配置
    'workermanServer' => 'workerman' === $mode ? [
        'http' => [
            'namespace' => 'ImiApp\WebSocketServer',
            'type'      => Imi\Workerman\Server\Type::HTTP,
            'host'      => '0.0.0.0',
            'port'      => 8080,
            'configs'   => [
                'count' => 1,
            ],
        ],
        'channel' => [
            'namespace'   => '',
            'type'        => Imi\Workerman\Server\Type::CHANNEL,
            'host'        => '127.0.0.1',
            'port'        => 13005,
            'configs'     => [
            ],
        ],
        // 以下是 Workerman Gateway 模式需要
        'register' => [
            'namespace'   => 'Imi\WorkermanGateway\Test\AppServer\Register',
            'type'        => Imi\WorkermanGateway\Workerman\Server\Type::REGISTER,
            'host'        => '127.0.0.1',
            'port'        => 13004,
            'configs'     => [
            ],
        ],
        'gateway' => [
            'namespace'   => 'Imi\WorkermanGateway\Test\AppServer\Gateway',
            'type'        => Imi\WorkermanGateway\Workerman\Server\Type::GATEWAY,
            'socketName'  => 'websocket://0.0.0.0:8081', // 网关监听的地址
            'configs'     => [
                'lanIp'           => '127.0.0.1',
                'startPort'       => 12900,
                'registerAddress' => '127.0.0.1:13004',
            ],
        ],
        // workerman gateway 模式 Worker
        'websocketWorker' => [
            'namespace'   => 'ImiApp\WebSocketServer',
            'type'        => Imi\WorkermanGateway\Workerman\Server\Type::BUSINESS_WEBSOCKET,
            'shareWorker' => '\\' === \DIRECTORY_SEPARATOR ? 'http' : null,
            'configs'     => [
                'registerAddress' => '127.0.0.1:13004',
                'count'           => 2,
            ],
            'beans' => [
                'ServerUtil' => 'WorkermanGatewayServerUtil',
            ],
        ],
    ] : [],
]

[
    'beans' => [
        // 网关模式
        'ConnectionContextStore'   => [
            'handlerClass'  => 'ConnectionContextGateway',
        ],
        'ServerGroup' => [
            'groupHandler' => 'GroupGateway',
        ],
    ],
]