Swoole\Server::addlistener ( string $host , integer $port , string $socket_type )
Alias: swoole_server->addListener( string $host , integer $port , string $socket_type )
<?php
mixed Swoole\Server->addListener(string $host, int $port, $type = SWOOLE_SOCK_TCP);
Add more listening IP or port for the server. The connection information can be acccessed by $server->connection_info()
when the server has started. By the connection information, you can distinguish the source port of the connection.
$host
: the ip address of the server$port
: the port of the server
$port
will be ignored when the parameter $sock_type
is setted to SWOOLE_UNIX_DGRAM
or SWOOLE_UNIX_STREAM
$port
is setted to 0
, the swoole server would use a random and available port$sock_type
: the socket type of the server:
Enable SSL: $sock_type | SWOOLE_SSL
. To enable ssl, it must set the configuration about ssl.
The swoole_server_port object
<?php
$server->addlistener("127.0.0.1", 9502, SWOOLE_SOCK_TCP);
$server->addlistener("192.168.1.100", 9503, SWOOLE_SOCK_TCP);
$server->addlistener("0.0.0.0", 9504, SWOOLE_SOCK_UDP);
//UnixSocket Stream
$server->addlistener("/var/run/myserv.sock", 0, SWOOLE_UNIX_STREAM);
//TCP + SSL
$server->addlistener("127.0.0.1", 9502, SWOOLE_SOCK_TCP | SWOOLE_SSL);
//Listen on a random port
$port = $server->addListener("0.0.0.0", 0, SWOOLE_SOCK_TCP);
echo $port->port;