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

Reconnect on error / disconnect #12

Open
korhaldragonir opened this issue Jan 29, 2020 · 5 comments
Open

Reconnect on error / disconnect #12

korhaldragonir opened this issue Jan 29, 2020 · 5 comments

Comments

@korhaldragonir
Copy link

I am currently using this library to build a Bot to run automation tasks.

However when it loses connectivity because either the server became unavailable or was restarted it does not automatically reconnect.

Is there a way for me to get the status and if the connection is no longer working to trigger a reconnect?

So far I have not been able to find a reliable way to do this.

Thanks!

@Norgul
Copy link
Collaborator

Norgul commented Jan 30, 2020

Hello. If you take a look at the codebase, there is a method to reconnect in XmppClient class if some error occurs, however this can be done if error response actually comes from server. Mostly what I've encountered was that socket connection breaks, however I was unable so far to get a reliable method to check whether socket connection is active.

I would appreciate it if you find a solution to post it as a pull request on repo. In the meantime I will look for solutions when I get the time.

@korhaldragonir
Copy link
Author

Thanks, I will take a look at it. If I get it fixed I will submit a pull request.

No rush, I do this in my spare time also. So I am not sure when exactly I get the time to come around to this.

Thank you again for your answer.

@korhaldragonir
Copy link
Author

korhaldragonir commented Feb 5, 2020

I have gotten a bit further. I noticed that when the server connection is interrupted I get a lot of SSL errors and error on the fwrite call in the Socket.php:52.

On line 52 in the src/Socket.php I changed the try statement to this:

            $fwrite_ret = fwrite($this->connection, $xml);
            if($fwrite_ret === 0)
            {
                // Connection died, we need to restart it.
               echo "[DEBUG] Socket dead!\n";
            }

            $this->options->getLogger()->logRequest(__METHOD__ . '::' . __LINE__ . " $xml");
            //$this->checkSocketStatus();

And the echo starts showing up the moment the connection get's interrupted. I noticed that on a stable / idle connection I receive int(11) as return value of the fwrite and only when there was no connection I saw a int(0) return value.

So that is why I checked for that. However I cannot seem to figure out how to tell the rest of the library from that moment on that the socket is dead.


Some more research, the reason this works is because within my while loop, before I start processing received messages I do: $client->iq->ping();

I do that to check my connection before I start processing or sending data. However no matter what the case I noticed that if you get an int(0) as a return from a fwrite over a network socket it is always bad.

So my while loop looks more like this:

        do {
            sleep(1);
            $client->iq->ping();
            $response = $client->getResponse();
            $client->prettyPrint($response);
        } while (true);

I sleep every 1 second as not to overwhelm the server and by using the ping I basically check if I can still send data.

@korhaldragonir
Copy link
Author

I have worked around it. I places some return values in the src/Socket.php and in the src/XML/Stanzas/Iq.php and check on it in the while loop in the Example.php.

Now when the connection breaks I exit with an error-code of 1, which the OS or Parent Process can catch and decide what to do.

Because on Linux I am using Systemd I have set it to Restart=always, since it sees exit-code 1 as abnormal it will restart the bot.

I will make a Pull request for this, and leave it to you to decide whether it is something you would like to implement in the library.

My apologies if the code is not 100% up to par, but everything I know about programming is self-taught.

@korhaldragonir
Copy link
Author

I submitted #13 to help with some error-handling.

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

No branches or pull requests

2 participants