xmpp-php
xmpp-php copied to clipboard
Detect connection interruption and exit with error code.
Added return false
to:
-
src/Socket.php
insend()
-
src/XML/Stanzas/Iq.php
inping()
In Example.php
:
Added some logic to detect problems with the socket and then exit with error code 1.
Even with a no operation you should at least have some payload in a packet. I just use the ping to check if I can still send a ping to the server. If that is not succesfully written then I cannot reach the server anymore.
I am not really sure there is any other way, because if the connection get's interrupted server side it will never tell you because the connection is dead.
And since the server won't re-connect, not the task of the server in a cilent-server architecture, the client will need to do so. However somehow I need to find out that the socket has gone stale.
So for that reason I explicitly send data, being ping, to the send method and see if the fwrite() fails. If fwrite fails to write to the socket something must have gone wrong.
Not saying it is perfect, but just trying to explain my train of thought.
Please if there is a better way to do it, by all means go ahead. But I needed a resolution and this works for me like a charm. Though by no means do I think it is a perfect way.
At what point are you doing the ping? Is it periodically?
Anyway, I will not discard your pull request yet as I see your point here. I will try a few more options and then pick and choose and let you know if I came to some smart conclusion.
Thanks!
That part is in the Example.php
do {
sleep(1); // Make sure to back off at least 1 second to give the server some time to breath.
if($client->iq->ping() === false) // Check if the connection is still alive by sending a ping.
{
$error = 1; // If it fails set the error code to 1, this will break the loop and tell the program to exit with code 1.
}
$response = $client->getResponse();
$client->prettyPrint($response);
} while (true);
But I see that I have forgotten to change the while statement. It should have been something like: while ($error != 1)
.
Before processing it will always send a ping and because of the sleep(1), it won't flood the server too bad.
Please let me know if you find a more elegant way of fixing this :-). You're welcome ;-) !