Swoole\Server::__construct

Declaration

<?php
Swoole\Server::__construct ( string $host [, int $port [, int $mode [, int $sock_type ]]] )

Parameters

host

The IP address of the server

port

The port of the server (root privilege requried if the port number is less than 1024)

mode

The running mode of the server:

  • SWOOLE_PROCESS: multiple process mode, the business logic is running in child processes, the default running mode of server
  • SWOOLE_BASE: reactor based mode, the business logic is running in the reactor
sock_type

The socket type of the server:

  • SWOOLE_SOCK_TCP: TCP
  • SWOOLE_SOCK_TCP6: TCP IPv6
  • SWOOLE_SOCK_UDP: UDP
  • SWOOLE_SOCK_UDP6: UDP IPv6
  • SWOOLE_UNIX_DGRAM: Unix socket dgram
  • SWOOLE_UNIX_STREAM: Unix socket stream
  • Enable SSL: $sock_type | SWOOLE_SSL. To enable SSL, check the configuration about SSL

Return

server

A Swoole\Server object

Description

Create a Swoole Server object

Example

<?php
$http = new Swoole\HTTP\Server("127.0.0.1");

$http->on('request', function ($request, $response) {
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});

$http->start();

Other

Listening a random port

The swoole supports the feature of listening on a random port. When the argument $port isn't seted or is 0, the swoole will choose a random and available port to listen on. You can use $server->port to listen on.

Systemd setup for Swoole Server

The swoole-1.9.7 adds the support for systemd socket. The port listened on can be setted by the configuration of systemd.

swoole.socket

[Unit]
Description=Swoole Socket

[Socket]
ListenStream=9501
Accept=false
[Install]
WantedBy = sockets.target

swoole.service

[Service]
Type=forking
PIDFile=/var/run/swoole.pid
ExecStart=/usr/bin/php /var/www/swoole/server.php
ExecStop=/bin/kill $MAINPID
ExecReload=/bin/kill -USR1 $MAINPID

[Install]
WantedBy = multi-user.target

server.php

<?php
$http = new Swoole\HTTP\Server("systemd");

$http->set([
    'daemonize' => true,
    'pid_file' => '/var/run/swoole.pid',
]);

$http->on('request', function ($request, $response) {
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});

$http->start();

Start the systemd service

sudo systemctl enable swoole.socket
sudo systemctl start swoole.socket
sudo systemctl start swoole.service