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'),
        ],
    ], 

Enable http request processing

The default is that the http server function is not enabled. If you want to open ws, handle http requests at the same time.

     // ...
    '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, now IP:PORT can handle both http and ws requests.

Add RPC service

If you want to enable ws, start the RPC Server service at the same time.

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

Enable all features

  • 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