Websocket configuration

The websocket's host, port and other configurations are completely customizable. The configuration needs to edit the app/bean.php file. Here are some simple configurations. You can also freely combine and provide multiple services.

The default port for websocket server is 18308

Configurable item

The configurable item is used for the ws server bean configuration, except that the class is the property of the ws server.

  • class specifies the processing class of the websocket server
  • port specifies the port of the websocket server
  • listener specifies other services that are started together, and adds port service listeners, which can be multiple.
    • Rpc starts RPC service
  • on configuration listen event
    • Register events, set up processing listeners for corresponding events, event trigger component calls, use in tasks
  • setting here is the reference Swoole Server Configuration Options
  • pidFile sets the process pid文件 location, the default value is @runtime/swoft.pid
  • mode running mode, refer to the third parameter of the Swoole Server constructor
  • type specifies the type of Socket, supports the fourth parameter of the Swoole Server constructor such as TCP, UDP, TCP6, UDP6, UnixSocket Stream/Dgram, etc.

Basic configuration

    // ...
    'wsServer'   => [
        'class'   => WebSocketServer::class,
        'port' => 18307,
        'debug' => env('SWOFT_DEBUG', 0),
        /* @see WebSocketServer::$setting */
        'setting' => [
            'log_file' => alias('@runtime/swoole.log'),
        ],
    ],

启用http请求处理

默认的是没有启用http server功能的。如果你想开启ws时,同时处理http请求。

    // ...
    'wsServer'   => [
        'class'   => WebSocketServer::class,
        'on'      => [
            // 加上如下一行,开启处理http请求
            SwooleEvent::REQUEST => bean(RequestListener::class),
        ],
        'debug' => env('SWOFT_DEBUG', 0),
        /* @see WebSocketServer::$setting */
        'setting' => [
            'log_file' => alias('@runtime/swoole.log'),
        ],
    ],

ok, 现在 IP:PORT 上可以同时处理 http 和 ws 请求了。

添加RPC服务

如果你想开启ws时,同时启动RPC Server服务。

    // ...
    'wsServer'   => [
        'listener' => [
            'rpc' => \bean('rpcServer') // 引入 rpcServer
        ],
    ],
    'rpcServer'  => [
        'class' => ServiceServer::class,
        'port' => 18308,
    ],

启用全部功能

  • websocket server
  • http server
  • task process
  • rpc server
    // ...
    'wsServer'   => [
        'class'   => WebSocketServer::class,
        'port' => 18307,
        'on'      => [
            // 开启处理http请求支持
            SwooleEvent::REQUEST => bean(RequestListener::class),
            // 启用任务必须添加 task, finish 事件处理
            SwooleEvent::TASK   => bean(TaskListener::class),  
            SwooleEvent::FINISH => bean(FinishListener::class)
        ],
        'listener' => [
            // 引入 rpcServer
            'rpc' => \bean('rpcServer')
        ],
        'debug' => env('SWOFT_DEBUG', 0),
        /* @see WebSocketServer::$setting */
        'setting' => [
            'log_file' => alias('@runtime/swoole.log'),
            // 任务需要配置 task worker
            'task_worker_num'       => 2,
            'task_enable_coroutine' => true
        ],
    ],
    'rpcServer'  => [
        'class' => ServiceServer::class,
        'port' => 18308,
    ],

Ok, now the server started by php bin/swoft ws:start supports all the above functions.

/docs/2.x/en/websocket-server/config.html
progress-bar