Open chrispri opened 4 years ago
Second parameter in send() is opcode. When sending ping
, the text message doesn't matter.
$client->send('Ping message', 'ping');
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?
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.
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.
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.
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();```
@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.
I've just opened a PR that helps with this - #140
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());
}
}
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?