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

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\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 OpenSwoole\Coroutine\Client;

co::run(function()
{
    $client = new Client(OpenSwoole\Constant::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 OpenSwoole\n");

    echo $client->recv();

    $client->close();
});
Last updated on September 20, 2022