Swoole\Coroutine\Client->isConnected(...)

Latest version: pecl install openswoole-4.11.1

Declaration

<?php Swoole\Coroutine\Client->isConnected(): bool

Parameters

None.

Return

Returns true when the client is connected to the server and false when the client is not currently connected to the server


Description

Check if the client connection is currently established and not disconnected.

This method returns the status of the connection on an application layer basis, meaning it is the status of if connect() has been performed successfully or not. Once you have called connect() successfully it cannot be called again, this would result in an error, you must close a connection first. However, the connection status from isConnected() is purely at the application level, this is not the actual TCP state. Errors can still appear when sending or receiving data via the client even if the isConnected() method returns true as something might have happened between that time and when data is sent or received. You don't truly know if a connection is available or not until you try to send or receive data, as that is a kernel operation.

Example

<?php

use Swoole\Coroutine\Client;

Co\run(function()
{
    $client = new Client(SWOOLE_SOCK_TCP);

    if(!$client->connect('127.0.0.1', 9501, 0.5))
    {
        echo "Connection failed with error: {$client->errCode}\n";
    }

    // Should not rely on this 100%, see description as to why
    if($client->isConnected() === false)
    {
        echo "Connection failed with error: {$client->errCode}\n";
    }

    $client->send("Hello World! - from Swoole\n");

    echo $client->recv();

    $client->close();
});
Last updated on March 31, 2022