Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to send a ping every x seconds? #89

Open
chrispri opened this issue Jun 26, 2020 · 9 comments
Open

How to send a ping every x seconds? #89

chrispri opened this issue Jun 26, 2020 · 9 comments

Comments

@chrispri
Copy link

chrispri commented Jun 26, 2020

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?

@chrispri chrispri changed the title How go send a ping every x seconds? How to send a ping every x seconds? Jun 26, 2020
@sirn-se
Copy link
Contributor

sirn-se commented Jun 26, 2020

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

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

@chrispri
Copy link
Author

chrispri commented Jun 26, 2020

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
Copy link
Contributor

sirn-se commented Jun 26, 2020

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
Copy link
Author

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
Copy link
Contributor

sirn-se commented Jun 26, 2020

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
Copy link

apeman76 commented May 10, 2021

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
Copy link
Author

@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
Copy link
Contributor

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

@ADzaka
Copy link

ADzaka commented Oct 9, 2022

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());
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants