Listening On Multiple Ports

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

OpenSwoole server supports listening on multiple ports and analyzing multiple protocols.

You can set different protocols and callback functions on different ports. SSL/TLS can also enabled on certain port.

  • If the listening on new port hasn't setted the protocal, it will be in the no protocal mode.

  • If the listening on new port hasn't setted the callback functions, it will use the callback functions of $server.

  • The return of $server->listen is object of swoole_server_port

  • The callback functions setted for different ports still run in same worker process.

Listen on new port

<?php
$port1 = $server->listen("127.0.0.1", 9501, OpenSwoole\Constant::SOCK_TCP);
$port2 = $server->listen("127.0.0.1", 9502, OpenSwoole\Constant::SOCK_UDP);
$port3 = $server->listen("127.0.0.1", 9503, OpenSwoole\Constant::SOCK_TCP | OpenSwoole\Constant::SSL);

Set protocol

<?php
$port1->set([
    'open_length_check' => true,
    'package_length_type' => 'N',
    'package_length_offset' => 0,
    'package_max_length' => 800000,
]);

$port3->set([
    'open_eof_split' => true,
    'package_eof' => "\r\n",
    'ssl_cert_file' => 'ssl.cert',
    'ssl_key_file' => 'ssl.key',
]);

Set callback functions of events

<?php
$port1->on('connect', function ($serv, $fd){
    echo "Client:Connect.\n";
});

$port1->on('receive', function ($serv, $fd, $from_id, $data) {
    $serv->send($fd, 'OpenSwoole: '.$data);
    $serv->close($fd);
});

$port1->on('close', function ($serv, $fd) {
    echo "Client: Close.\n";
});

$port2->on('packet', function ($serv, $data, $addr) {
    var_dump($data, $addr);
})

Optional configurations for OpenSwoole\Server\Port object

The OpenSwoole\Server\Port object can only set part of the configurations list of swoole_server

  • If the OpenSwoole\Server\Port object hasn't set any configuration, it will use the configurations of the main OpenSwoole server object.

  • If the main server object is instance of OpenSwoole\HTTP\Server or OpenSwoole\WebSocket\Server and the OpenSwoole\Server\Port object hasn't setted the configuration about protocal, the OpenSwoole\Server\Port is setted to analyze http or websocket protocol automatically and can't be setted the custom callback funtion of event receive.

  • If the main server object is instance of OpenSwoole\HTTP\Server or OpenSwoole\WebSocket\Server and the OpenSwoole\Server\Port object has setted some custom configurations, the OpenSwoole\Server\Port object would change to analyze TCP protocol. If you still want to set the OpenSwoole\Server\Port object to analyze the Http/WebSocket protocol, it needs to add open_http_protocol => true or open_websocket_protocol => true

Available Configurations for OpenSwoole\Server\Port object:

  • socket configurations, for example, backlog, TCP_KEEPALIVE, open_tcp_nodelay, tcp_defer_accept, etc

  • protocol configurations, for example, open_length_check, open_eof_check, package_length_type, etc

  • ssl configurations, for example, ssl_cert_file、ssl_key_file, etc

Unavailable Configurations for OpenSwoole\Server\Port object:

  • worker_num, task_worker_num, reactor_num

  • dispatch_mode, task_ipc_num

  • heartbeart_check

  • log_file

  • user, group, chroot

  • open_cpu_affinity

  • max_request, task_max_request

Optional callback functions for `OpenSwoole\Server\Port object

The OpenSwoole\Server\Port object can only set part of the callback functions of OpenSwoole\Server

For TCP server:

  • onConnect

  • onClose

  • onReceive

For UDP server:

  • onPacket

  • onReceive

The below callback functions can only be setted by `OpenSwoole\Server object.

  • onStart

  • onShutdown

  • onWorkerStart

  • onWorkerStop

  • onManagerStart

  • onManagerStop

  • onTask

  • onFinish

  • onPipeMessage

  • onWorkerError

Q&A

The OpenSwoole\HTTP\Server class and OpenSwoole\WebSocket\Server class inherit the OpenSwoole\Server class. The OpenSwoole\Server object can't call listen method to create `OpenSwoole\Server\Port object of Http or Websocket server.

In this situation, you can create the OpenSwoole\HTTP\Server object and then call listen method to create tcp server.

<?php
$http_server=new OpenSwoole\HTTP\Server('0.0.0.0',9998);
$http_server->set(array('xxx'=>'yyy'));
$http_server->on('request','request');
$tcp_server=$http_server->addListener('0.0.0.0',9999,OpenSwoole\Constant::SOCK_TCP);
Last updated on September 20, 2022