Textalk / websocket-php

WebSocket client and server in PHP
Other
925 stars 253 forks source link

How to send a ping every x seconds? #89

Open chrispri opened 4 years ago

chrispri commented 4 years ago

I use the gardena websocket (smart API) which closes the connection every 200 seconds even I send no ping message. How can I get this work?

sirn-se commented 4 years ago

Second parameter in send() is opcode. When sending ping, the text message doesn't matter.

$client->send('Ping message', 'ping');
chrispri commented 4 years ago

Second parameter in send() is opcode. When sending ping, the text message doesn't matter.

$client->send('Ping message', 'ping');

Thanks for answer. But this isn´t possible when I am receiving data without async? Am I wrong?

sirn-se commented 4 years ago

Correct, the receive() operation is blocking. So if you are waiting for input but the server doesn't write anything for 200 seconds the connection will close.

chrispri commented 4 years ago

Thats bad. Do you have any ideas how i can get this work?

Here is the manual of Gardena/Husquvarna: https://developer.husqvarnagroup.cloud/apis/GARDENA+smart+system+API#/readme

And thats the text part which makes problems: Make a WebSocket connection using the obtained URL. Since the WebSocket is closed automatically after 300 seconds of inactivity, we recommend sending ping messages every 150 seconds to keep the connection open.

sirn-se commented 4 years ago

Only thing to do is to expect socket to close and have it re-connect. As that API allows http-requests as well it should not be a problem.

I will consider this use case for further development, though.

apeman76 commented 3 years ago

What about this:


    $client = new WebSocket\Client("wss://URL", [
        'timeout' => 5
    ]);
    $lastSend = round(microtime(true) * 1000);
    while (true) {
        try {
            $time = round(microtime(true) * 1000);
            if ($time > $lastSend + 4.9 * 1000) {
                $lastSend = round(microtime(true) * 1000);
                $client->send('ping', 'ping');
            }
            $data = $client->receive();
            if ($data) {
                print_r($data);
            }
        } catch (\WebSocket\ConnectionException $e) {
            print_r($e);
        }
    }
    $client->close();```
chrispri commented 3 years ago

@mrsnut I tried a similar code but this can't work because the server doesn't send data the most time. So the while loop stops at "$data = $client->receive();" until the timeout closes the connection.

marcroberts commented 3 years ago

I've just opened a PR that helps with this - #140

ADzaka commented 2 years ago

I found an easier method. When you set up the client, set a timeout for the connection (one of the options parameters), like this: $client = new \WebSocket\Client(getenv('WS_REALTIME_URL_SPOT'), ['timeout' => 20]);

After that, in the catch where all the websocket errors are caught, you will get a timeout after not receiving data, every 20 seconds. Use this timeout to send a heartbeat.

} catch (\WebSocket\ConnectionException $e) {
        $dateStartPlusOneMinute = Carbon::now()->addSeconds(70);
        if ($e->getMessage() == "Client read timeout") {
               $client->text('{"op":"ping"}');
        } else {
               Log::info("Foutmelding: ".$e->getMessage());
        }
}