Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Server->confirm(int $fd): bool
The file descriptor ID of the client that you want to confirm the connection for. You can get the fd
from the onConnect
callback event.
If successful, true
is returned, otherwise false
for when the client connection does not exist or is already in a monitoring state.
Confirm a client connection and use it in conjunction with enable_delay_receive
server configuration option.
When the client establishes a connection, do not listen for readable events (or data), only trigger the onConnect
event callback when the client connects and proceed to confirm their connection, once the connection is confirmed then the server will listen for readable event data that is received from client connection, allowing the server to accept client data.
This method is generally used to protect servers and avoid traffic overload attacks. The onConnect
function is triggered when a client connection is received , which can determine the source IP and whether it is allowed to send data to the server. You can delay the client before their connection is added to the event loop.
Available since v4.5.0
<?php
// Create a new Server object and listen on port 127.0.0.1:9501
$server = new OpenSwoole\Server("127.0.0.1", 9501);
$server->set([
'enable_delay_receive' => true,
]);
// Listen for connection entry events
$server->on('Connect', function ($serv, $fd)
{
// Check the client $fd here and then confirm if there is no problem
$serv->confirm($fd);
});
// Only process client data when the connection has been confirmed
$server->on('Receive', function ($serv, $fd, $reactor_id, $data)
{
$serv->send($fd, "Server: ".$data);
});
// Listen for connection close events
$server->on('Close', function ($serv, $fd)
{
echo "Client: Close.\n";
});
// Start the server
$server->start();