Swoole Server configurations can be set with $server->set
.
<?php
$server->set([
// process
'daemonize' => 1,
'user' => 'user',
'group' => 'user',
'chroot' => '/data/server/',
'open_cpu_affinity' => true,
'cpu_affinity_ignore' => [0, 1],
'pid_file' => __DIR__.'/server.pid',
// server
'reactor_num' => 8,
'worker_num' => 2,
'message_queue_key' => 'mq1',
'dispatch_mode' => 2,
'discard_timeout_request' => true,
'dispatch_func' => 'my_dispatch_function',
// worker
'max_request' => 0,
'max_request_grace' => $max_request / 2,
// task worker
'task_ipc_mode' => 1,
'task_max_request' => 100,
'task_tmpdir' => '/tmp',
'task_worker_num' => 8,
'task_enable_coroutine' => true,
'task_use_object' => true,
// logging
'log_level' => 1,
'log_file' => '/data/swoole.log',
'log_rotation' => SWOOLE_LOG_ROTATION_DAILY | SWOOLE_LOG_ROTATION_SINGLE,
'log_date_format' => true, // or "day %d of %B in the year %Y. Time: %I:%S %p",
'log_date_with_microseconds' => false,
'request_slowlog_file' => fasle,
// tcp
'buffer_output_size' => 32*1024*1024, // byte in unit
'tcp_fastopen' => false,
'max_conn' => 1000,
'tcp_defer_accept' => true,
'open_tcp_keepalive' => true,
'open_tcp_nodelay' => false,
'pipe_buffer_size' => 32 * 1024*1024,
'socket_buffer_size' => 128 * 1024*1024,
// kernel
'backlog' => 512,
'kernel_socket_send_buffer_size' => 65535,
'kernel_socket_recv_buffer_size' => 65535,
// tcp parser
'open_eof_check' => true,
'open_eof_split' => true,
'package_eof' => '\r\n',
'open_length_check' => true,
'package_length_type' => 'N',
'package_body_offset' => 8,
'package_length_offset' => 8,
'package_max_length' => 81920,
'package_length_func' => 'my_package_length_func',
// coroutine
'enable_coroutine' => true,
'max_coroutine' => 3000,
'send_yield' => false,
// tcp server
'heartbeat_idle_time' => 600,
'heartbeat_check_interval' => 60,
'enable_delay_receive' => true,
'enable_reuse_port' => true,
'enable_unsafe_event' => true,
// protocol
'open_http_protocol' => true,
'open_http2_protocol' => true,
'open_websocket_protocol' => true,
'open_mqtt_protocol' => true,
// ssl
'ssl_cert_file' => __DIR__ . '/config/ssl.cert',
'ssl_key_file' => __DIR__ . '/config/ssl.key',
'ssl_ciphers' => 'ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP',
'ssl_method' => SWOOLE_SSLv3_CLIENT_METHOD, // removed from v4.5.4
'ssl_protocols' => 0, // added from v4.5.4
'ssl_verify_peer' => false,
'ssl_sni_certs' => [
"cs.php.net" => [
'ssl_cert_file' => __DIR__ . "/config/sni_server_cs_cert.pem",
'ssl_key_file' => __DIR__ . "/config/sni_server_cs_key.pem"
],
"uk.php.net" => [
'ssl_cert_file' => __DIR__ . "/config/sni_server_uk_cert.pem",
'ssl_key_file' => __DIR__ . "/config/sni_server_uk_key.pem"
],
"us.php.net" => [
'ssl_cert_file' => __DIR__ . "/config/sni_server_us_cert.pem",
'ssl_key_file' => __DIR__ . "/config/sni_server_us_key.pem",
],
],
// static files
'enable_static_handler' => true,
'document_root' => __DIR__ . '/public',
'static_handler_locations' => ['/static', '/app/images'],
// source file reloading
'reload_async' => true,
'max_wait_time' => 30,
// http server
'http_parse_post' => true,
'http_parse_cookie' => true,
'upload_tmp_dir' => '/tmp',
// compression
'http_compression' => true,
'http_compression_level' => 3, // 1 - 9
'http_gzip_level' => 1,
'compression_min_length' => 20,
// websocket
'websocket_compression' => true,
'open_websocket_close_frame' => false,
'open_websocket_ping_frame' => false, // added from v4.5.4
'open_websocket_pong_frame' => false, // added from v4.5.4
// TCP_USER_TIMEOUT
'tcp_user_timeout' => 0,
'dns_server' => '8.8.8.8:53',
'dns_cache_refresh_time' => 0,
'enable_preemptive_scheduler' => 0,
'open_fastcgi_protocol' => 0,
'open_redis_protocol' => 0,
'input_buffer_size' => 2097152,
'output_buffer_size' => 2097152,
'stats_file' => './stats_file.txt',
])
Allow a number of waiting connections.
The configuration is an integer representing the number of pending connections that wait for accept.
Set the output buffer size in the memory.
The default value is 2M. The data to send can't be larger than buffer_output_size
every time.
<?php
$server->set([
'buffer_output_size' => 32 * 1024*1024, // byte in unit
])
Redirect the root path of the worker process.
This configuration is to separate the operation to the file system in the worker process from the real file system.
<?php
$server->set([
'chroot' => '/data/server/'
]);
By default, all the I/O interrupts are processed by CPU 0.
Set the CPU core number which are only used to handle the I/O interrupts.
<?php
[
'cpu_affinity_ignore' => [0, 1]
]
How to check I/O interrupts on CPU cores?
cat /proc/interrupts
Daemonize the swoole server process.
If the value of daemonize
is more than 1, the swoole server will be daemonized.
The program which wants to run a long time must enable this configuration.
If the daemonize
has been enabled, the standard output and error of the program will be redirected to logfile
. And if the configuration of log_file
hasn't been set, the standard output and error of the program will be redirected to /dev/null
.
After daemonizing the processes, the value of
CWD
will change and the relative path of the file will be different. So it must use the absolute path in the program.
If the configuration of dispatch_mode
is 1 or 3, there may be some data that arrive to the worker process after closing the connection.
In this situation, if discard_timeout_request
is true, the worker process will discard these data or still process these data.
Set the dispatch_func
to dispatch the connection to the worker process.
The swoole server provides [five types of dispatch_mode. If the dispatch_mode
can't meet your need, you can write C++ function or PHP function to realize customized dispatch strategy.
<?php
$server->set([
'dispatch_func' => 'my_dispatch_function',
]);
Once the
dispatch_func
has been setted, the configurationdispatch_mode
would not work.If the function set by
dispatch_func
, Swoole will result in a fatal error.If the data dispatched is more than 8K,
dispatch_func
can get 0-8180 bytes data.
It is forbidden to add any blocking operation in the
dispatch_func
otherwise theReactor
group would stop.
<?php
$server->set([
'dispatch_func' => function ($server, $fd, $type, $data) {
var_dump($fd, $type, $data);
return intval($data[0]);
}
]);
Parameters:
$fd
, the id number of client
$type
, the type of data, 0
: receive data from the client, 4
: the connection with the client closes, 5
: the connection with the client starts
$data
, the data to dispatch
Return:
the return must be an integer between 0 and worker_num
In other extension, use swoole_add_function
to register function to swoole
int dispatch_function(swServer *serv, swConnection *conn, swEventData *data);
int dispatch_function(swServer *serv, swConnection *conn, swEventData *data)
{
printf("cpp, type=%d, size=%d\n", data->info.type, data->info.len);
return data->info.len % server->worker_num;
}
int register_dispatch_function(swModule *module)
{
swoole_add_function("my_dispatch_function", (void *) dispatch_function);
}
The mode of dispatching connections to the worker process.
This parameter only works for the SWOOLE_PROCESS
mode swoole_server.
1
, round robin.2
, fixed mode (default mode of dispatch_mode
). Dispatch the connection to the worker according to the id number of connection. In this mode, the data from the same connection will be handled by the same worker process. 3
, preemptive mode. dispatch the connection to the idle worker process.4
, dispatch based on IP
. Dispatch the connection to the worker according to the IP of the client. The dispatch algorithm is ip2long(ClientIP) % worker_num
5
, dispatch based on user defined ID. If the connection has been bound with a uid by Swoole\Server->bind
, the swoole will dispatch the connection to the worker according to the uid. The dispatch algorithm is UID % worker_num
. If you want to use a string as a uid, it should convert this string by crc32($uid)
7
, stream
mode, use the idle worker process to accept and process the requests.stateless server: 3
is advised for synchronous and blocking server, 1
is advised for asynchronous and non-blocking server
stateful server: 2
, 4
, 5
If the
dispatch_mode
is1
or3
, the event ofconnect
andclose
will be shielded.
Enable the delay of receiving a new connection.
Once enabled, the client which has been accepted would not be added to the EventLoop automatically and only trigger the callback function of the received event.
The worker process needs to call $server->confirm($fd)
to confirm the connection manually and add the connection to the EventLoop.
<?php
// enable `enable_delay_receive`
$server->set([
'enable_delay_receive' => true,
]);
$server->on("Connect", function ($server, $fd, $reactorId) {
$server->after(2000, function() use ($server, $fd) {
// confirm connection and start process data
$server->confirm($fd);
});
});
Enable the reuse of port.
Enable the event of close and close when the dispatch_mode
is 1 or 3.
Set the group of worker and task worker process.
<?php
$server->set([
'group' => 'www-data'
]);
This configuration heartbeat_check_interval
is the interval of polling every TCP connection.
If the connection hasn't sent any data to the server in the last interval of heartbeat_check_interval
, the connection will be closed.
The swoole server would not send the heartbeat packet to the client but only wait for the heartbeat packet from the client. The heartbeat check of swoole server only checks the last time of sending data from the client. If the time exceeds heartbeat_check_interval
, the connection between the server and the client will be closed.
This is only for TCP server.
This configuration which works with the heartbeat_check_interval
stands for the max idle time of connection.
<?php
[
'heartbeat_idle_time' => 600,
'heartbeat_check_interval' => 60,
];
Set the log path of swoole.
In the log file, there are some labels to distinguish the thread or process that output the log item.
#
Master process
$
Manager process
*
Worker process
^
Task worker process
If the log file has been mv
or unlink
, the log can't be recorded normally. In this situation, you can send SIGRTMIN
Set the level of the log.
The log that is inferior to the log_level set will not be recorded to log file.
<?php
$server->set([
'log_level' => 1,
]);
0 =>DEBUG // all the levels of log will be recorded
1 =>TRACE
2 =>INFO
3 =>NOTICE
4 =>WARNING
5 =>ERROR
The max number of connection the server could handle at the same time.
After exceeding this limit, the swoole server will refuse the new coming connection.
max_conn
should be less thanulimit -n
max_conn
should be more than(server->worker_num + SwooleG.task_worker_num) * 2 + 32
the default value ofmax_conn
isulimit -n
the value ofmax_conn
should not be too large and be setted according to the memory usage of server.
A worker process is restarted to avoid memory leak when receving max_request
+ rand(0, max_request_grace)
requests.
The default value of max_request_grace
is (max_request
/ 2);
The default value of max_request
is 0 which means there is no limit of the max request. If the max_request
is set to some number, the worker process will exit and release all the memory and resource occupied by this process after receiving the max_request
request. And then, the manager will respawn a new worker process.
This parameter is to resolve the problem of nonlocalizable and slow memory leak.
max_request
only works for synchronous, blocking and stateless response program The asynchronous server should not setmax_request
max_request
only works forSWOOLE_PROCESS
mode.
<?php
$server = new swoole_server("127.0.0.1", 9501);
$server->set([
'worker_num' => 2,
'max_request' => 3,
'dispatch_mode'=>3,
]);
$server->on('receive', function ($server, $fd, $from_id, $data) {
$server->send($fd, "Server: ".$data);
});
$server->start();
Check the PID of the worker process before request and after many times request.
The default value of max_request_grace
is (max_request
/ 2);
A worker process is restarted to avoid memory leak when receving max_request
+ rand(0, max_request_grace)
requests.
You can disable the rand function
by setting max_request_grace = 0
;
Set the key of the message queue.
This configuration only works if the configuration of task_ipc_mode
has been set to 2
or 3
.
The default value of message_queue_key
is calculated by ftok($php_script_file, 1)
.
You can check the data in message queue with command:
ipcs -q
ipcrm -Q [msgkey]
Open the affinity setting of CPU.
By enabling open_cpu_affinity
, the worker process is bonding with a CPU core to increase the CPU cache hit rate.
How to check if the process is bonding with a CPU core?
taskset -p PID
pid 24666\'s current affinity mask: f
pid 24901\'s current affinity mask: 8
Check more about CPU affinity and taskset
This configuration is to check the package EOF. The package EOF is set by the configuration package_eof
If this configuration has been enabled, the swoole will check the end of data from the client. If the end of data from the client is the string set by package_eof
, the swoole will send the data to the worker process otherwise the swoole will continue to receive and joint the data from the client.
<?php
[
'open_eof_check' => true,
'package_eof' => "\r\n",
]
The above configuration example can be used for Memcache or POP protocal which ends by \r\n
. Once setted, the worker process will receive one or serveral whole packages. In the worker process, you should use explode("\r\n", $data)
split the data or set the configuration package_eof
to split the data automatically.
Enable the split of data to package.
Once the configuration open_eof_check
has been set, the worker process will receive the data ending with the specified string. This data may contain one or serval whole package. In this situation, you can enable the open_eof_split
to split the data to package automatically. And then the callback function onReceive
will receive a whole package.
Enable the process of http2 protocol.
This configuration needs to compile swoole with --enable-http2
<?php
$server->set([
'open_http2_protocol' => true
]);
Enable the HTTP protocol.
Once this configuration has enabled, the callback function onReceive
will receive a whole HTTP package.
The swoole_http_server will enable this configuration automatically.
<?php
$server->set([
'open_http_protocol' => true
]);
Open the check of package length.
If this configuration has enabled, the swoole will open the analysis of package which has fixed header and body. And the worker process will receive a whole package in the callback function onReceive
.
This configuration should work with three other configurations.
Use some field in the header of the package to stand for the length the package. The swoole supports 10 types of length. Check the full list in the configuration of package_length_type
.
The offset of the package to calculate the length of the package.
0
, the length stands for the length of header and bodyN
, this value stands for that the length of the header is N and the length of a package only contains the length of the body.The offset of the value of length
in the header
struct
{
uint32_t type;
uint32_t uid;
uint32_t length;
uint32_t serid;
char body[0];
}
The configuration designed from the above protocal design
<?php
$server->set(array(
'open_length_check' => true,
'package_max_length' => 81920,
'package_length_type' => 'N',
'package_length_offset' => 8,
'package_body_offset' => 16,
));
Enable the process of MQTT
protocol.
Once this configuration has enabled, the swoole will analyze the MQTT
package head and the callback function onReceive
will receive a whole MQTT
package.
<?php
$server->set([
'open_mqtt_protocol' => true
]);
Open this configuration to close the Nagle algorithm.
Enable the process of WebSocket protocol.
Swoole enables this configuration open_http_protocol
automatically if the configuration open_websocket_protocol
has been enabled.
The swoole_websocket_server will enable this configuration automatically.
<?php
$server->set([
'open_websocket_protocol' => true
]);
Set the end string of package.
This configuration should work with open_eof_check
.
The max length of this string is 8 bytes.
Set the function to check the length of the package. The swoole supports the PHP function and C++ function.
The function returns an integer.
0
, lack of data to joint a whole package
1
, error data, the swoole will close the connection.
The length of the package.
<?php
$server = new Swoole\Server("127.0.0.1", 9501);
$server->set([
'open_length_check' => true,
'dispatch_mode' => 1,
'package_length_func' => function ($data) {
if (strlen($data) < 8) {
return 0;
}
$length = intval(trim(substr($data, 0, 8)));
if ($length <= 0) {
return -1;
}
return $length + 8;
},
'package_max_length' => 32 * 1024*1024,
]);
$server->on('receive', function (Swoole\Server $serv, $fd, $from_id, $data){
var_dump($data);
echo "#{$server->worker_id}>> received length=" . strlen($data) . "\n";
});
$server->start();
The type of length. For now, the swoole supports 10 types.
The max length of a package whose unit is byte
.
Once the configuration open_length_check/open_eof_check/open_http_protocol
has enabled, the internal of swoole will joint the data received from the client and the data stores in the memory before receiving the whole package. So to limit the usage of memory, it should set the package_max_length
.
This size is also the max file size can be uploaded.
The file path which the master process id saves in.
<?php
$server->set([
'pid_file' => __DIR__.'/server.pid',
]);
The PID of master process saves in the pid_file
after the swoole server starts. And this file is deleted after the swoole server stops normally.
Set the buffer size of the pipe.
<?php
$server->set([
'pipe_buffer_size' => 32 * 1024 *1024,
]);
The number of the reactor threads.
You can change the number of I/O threads in the master process.
To utilise all the CPUs, the default number of reactor_num
is the number of core in CPU.
reactor_num
has to be smaller than the worker_num
.
Set the buffer size of the socket.
This configuration is to set the max memory size of the connection.
<?php
$server->set([
'socket_buffer_size' => 128 * 1024 *1024,
])
It must add
--enable-openssl
to the support for SSL when you compile the swoole
To enable SSL, it should set the file path of the cert file and the key file.
<?php
$server = new Swoole\Server("0.0.0.0", 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$server->set([
'ssl_cert_file' => __DIR__ . '/config/ssl.cert',
'ssl_key_file' => __DIR__ . '/config/ssl.key',
]);
Convert PEM to DER:
openssl x509 -in cert.crt -outform der -out cert.der
Convert DER to PEM:
openssl x509 -in cert.crt -inform der -outform pem -out cert.pem
Set the ssl_ciphers
to change the default encryption algorithm of OpenSSL. The default encryption algorithm of OpenSSL is EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
<?php
$server->on([
'ssl_ciphers' => 'ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP', // the swoole use the default algorithm when the `ssl_ciphers` is empty
]);
It must add
--enable-openssl
to the support for SSL when you compile the swoole
To enable SSL, it should set the file path of the cert file and the key file.
<?php
$server = new Swoole\Server("0.0.0.0", 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$server->set([
'ssl_cert_file' => __DIR__ . '/config/ssl.cert',
'ssl_key_file' => __DIR__ . '/config/ssl.key',
]);
Convert PEM to DER
openssl x509 -in cert.crt -outform der -out cert.der
Convert DER to PEM
openssl x509 -in cert.crt -inform der -outform pem -out cert.pem
This option is removed from v4.5.4, pleause use
ssl_protocols
instead.
Set the algorithm of SSL. The algorithm of client and server must be same otherwise the handshake of WebSocket will fail.
The default algorithm is SWOOLE_SSLv23_METHOD
<?php
$server->set([
'ssl_method' => SWOOLE_SSLv3_CLIENT_METHOD, // this configuration is available for the swoole whose version is higher than 1.7.20
]);
Supported methods:
SWOOLE_SSLv3_METHOD
SWOOLE_SSLv3_SERVER_METHOD
SWOOLE_SSLv3_CLIENT_METHOD
SWOOLE_SSLv23_METHOD
SWOOLE_SSLv23_SERVER_METHOD
SWOOLE_SSLv23_CLIENT_METHOD
SWOOLE_TLSv1_METHOD
SWOOLE_TLSv1_SERVER_METHOD
SWOOLE_TLSv1_CLIENT_METHOD
SWOOLE_TLSv1_1_METHOD
SWOOLE_TLSv1_1_SERVER_METHOD
SWOOLE_TLSv1_1_CLIENT_METHOD
SWOOLE_TLSv1_2_METHOD
SWOOLE_TLSv1_2_SERVER_METHOD
SWOOLE_TLSv1_2_CLIENT_METHOD
SWOOLE_DTLSv1_METHOD
SWOOLE_DTLSv1_SERVER_METHOD
SWOOLE_DTLSv1_CLIENT_METHOD
This option is added from v4.5.4
Set the SSL protocols. By default, all protocols are enabled.
Supported protocols:
SWOOLE_SSL_TLSv1
SWOOLE_SSL_TLSv1_1
SWOOLE_SSL_TLSv1_2
SWOOLE_SSL_TLSv1_3
SWOOLE_SSL_SSLv2
SWOOLE_SSL_SSLv3
<?php
$serv = new swoole_server('0.0.0.0', 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$serv->set(array(
'ssl_cert_file' => __DIR__.'/config/ssl.crt',
'ssl_key_file' => __DIR__.'/config/ssl.key',
'ssl_verify_peer' => true,
'ssl_allow_self_signed' => true,
'ssl_client_cert_file' => __DIR__ . '/config/client.crt',
'ssl_protocols' => SWOOLE_SSL_TLSv1_2 | SWOOLE_SSL_TLSv1_3 | SWOOLE_SSL_TLSv1_1 | SWOOLE_SSL_SSLv2,
));
Verify the SSL certificate from client side before establish the connection.
<?php
$serv = new swoole_server('0.0.0.0', 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$serv->set(array(
'ssl_cert_file' => __DIR__.'/config/ssl.crt',
'ssl_key_file' => __DIR__.'/config/ssl.key',
'ssl_verify_peer' => true,
'ssl_allow_self_signed' => true,
'ssl_client_cert_file' => __DIR__ . '/config/client.crt',
));
Server Name Indication (SNI) allows the server to safely host multiple TLS Certificates for multiple sites, all under a single IP address.
You can set multiple SSL certificate for different domains.
Set the communication mode between the task worker process and worker process.
1
, default mode, use UNIX socket to communicate2
, use the message queue to communicate3
, use the message queue to communicate and set the mode to competitionThe difference between mode
2
and3
: mode2
supports the feature of sending task to a specified task worker process by$server->task($data, $task_worker_id)
while the mode3
don't support to specify task worker process.
The message queue uses the memory queue provided by os to store the data.
If the configuration message_queue_key
hasn't been set, the message queue would use the private queue and this private queue will be deleted after the close of swoole server.
If the configuration message_queue_key
has been set, the data of message queue would not be deleted and the swoole server could get the data after a restart.
Use ipcrm -q message_queue_id
to delete the data of message queue
After handling the number of task_max_request
tasks, the task worker process will exit and release all the memory and resource used by this process. And then, the manager will respawn a new task worker process.
The default value of task_max_request
is 0 which means there is no limit of the max task request.
Set the path of temporary task data.
If size of task message exceeds 8192 bytes, swoole uses the temporary file to store the data.
The default of task_tmpdir
is /tmp
.
To enable this parameter, it needs to register the callback function of the task
event and finish
event.
The task worker process is synchronous and blocking.
According to the speed of sending task and handling task, set a reasonable value of task_worker_num
.
Enable coroutine support in task worker.
Use object to pass task data.
This configuration is to defer the TCP connection to trigger onReceive
events before receving data.
<?php
$server->set(array(
'tcp_defer_accept' => 5
));
Set the user of worker and task worker process.
<?php
$server->set(['user' => 'apache']);
The number of worker process
If the code of logic is asynchronous and non-blocking, set the worker_num
to the value from one time to four times of CPU cores.
If the code of logic is synchronous and blocking, set the worker_num
according to the consuming time of request and load of os.
By enabling reload_async
, the worker processes shutdown after processing all the pending events.
The max waiting time to restart a worker process.
By enabling tcp_fastopen
, send data with SYNC
. Check more about tcp_fastopen
Enable slowlog and set the location of log file.
<?php
$server->set([
'request_slowlog_file' => '/tmp/swoole_slowlog.log',
'request_slowlog_timeout' => 2,
'trace_event_worker' => true,
]);
Enable or disable POST body parsing.
Enable or disable Cookie parsing.
Enable or disable coroutine in the callback function.
Set the max coroutine number per worker process.
Yield the current task and waiting for I/O if the send buffer is full.
TCP level Keep-Alive checking.
<?php
$serv->set(array(
'worker_num' => 1,
'open_tcp_keepalive' => 1, // enable TCP Keep-Alive check
'tcp_keepidle' => 4, // check if there is no data for 4s.
'tcp_keepinterval' => 1, // check if there is data every 1s
'tcp_keepcount' => 5, //close the connection if there is no data for 5 cycles.
));
Enable or disable static files handling for the files in the static_handler_locations
.
The folders for static files.
The temp directory to save the uploaded files.
Enable or disable compression for HTTP response.
There are three types of compression: gzip
, br
, deflate
supported and used based on the Accept-Encoding
HTTP header from HTTP request.
gzip
and deflate
requires zlib
. You have to install it before installing Swoole:
sudo apt-get install libz-dev
br
requires brotli
from Google. You have to install it before installing Swoole:
sudo apt-get install libbrotli-dev
Set the HTTP compression level to be 1 - 9
.
To specify the minimum length of the response to compress, use the compression_min_length directive. The default is 20 bytes.
Enable or disable compression for websocket response.
This is disabled by default.
Pass the close frame from client to message callback function.
<?php
$server->on('message', function (swoole_websocket_server $server, $frame) {
if ($frame->opcode == 0x08) {
echo "Close frame received: Code {$frame->code} Reason {$frame->reason}\n";
} else {
echo "Message received: {$frame->data}\n";
}
});
Set a file location for Swoole server to priint the stats info to the file once per second.