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

Question, on breaks. Errors #1006, 1008 #41

Open
Konstantin-seo opened this issue Jul 13, 2022 · 5 comments
Open

Question, on breaks. Errors #1006, 1008 #41

Konstantin-seo opened this issue Jul 13, 2022 · 5 comments
Labels

Comments

@Konstantin-seo
Copy link

Konstantin-seo commented Jul 13, 2022

Periodically, 5-10 times per hour, my script crashes into a fatal error. Here is the log:

  • Connection closed abnormally while awaiting message; Code 1006 (ABNORMAL_CLOSE); Reason: "TCP connection closed unexpectedly"
  • Connection closed abnormally while awaiting message; Code 1008 (POLICY_VIOLATION); Reason: "Exceeded unanswered PING limit"

How can I rewrite the code so that the script does not fall into a fatal error, but just let's say it sleeps for 30 seconds? or maybe there is another way so that this error does not occur and the script runs forever. I'll attach the code, can you help write the correct workaround for this error?

I will be very grateful!


MyCode:

<?php

//require_once ...

use Amp\ByteStream\StreamException;
use Amp\Loop;
use Amp\Websocket;
use Amp\Websocket\Client;
use Amp\Websocket\Client\Connection;
use Amp\Websocket\Client\Handshake;
use Amp\Websocket\ClosedException;
use Amp\Websocket\Message;
use Amp\Websocket\Options;
use Amp\Delayed;
use function Amp\Websocket\Client\connect;
	
	Amp\Loop::run(function () {
	
	$handshake = (new Handshake('wss://mg-s1.site.ru/api/bot/v1/ws?events=message_new'))->withHeader('x-bot-token', '0000000000000000000000000000000000000000000000000');

    $connection = yield connect($handshake);
    yield $connection->send('Hello!');

    try {
		while ($message = yield $connection->receive()) {
			$payload = yield $message->buffer();
			
			$result = json_decode($payload, true);
			
			//handler code

		} 
	} catch (ClosedException $e) {
            logFileEvent ('Connection break. sleep 30 sec');
			logFile ('Errors: ' . $e->getMessage());
			sleep(30);
		} catch (AssertionError $e) {
            logFile ('Errors: ' . $e->getMessage());
            $connection->close();
        } catch (Error $e) {
			logFile ('Errors: ' . $e->getMessage());
            $connection->close();
        } catch (StreamException $e) {
			logFile ('StreamException: ' . $e->getMessage());
            $connection->close();
        }
});

?>
@Konstantin-seo
Copy link
Author

@kelunik
Can you explain why these 2 errors occur and where should I dig to avoid them?

For example, error 1006 occurs for me more than 100 times a day. For some reason, I immediately raise a new connection, but I want to understand the reason for their appearance. Is it my server? library settings?

What options need to be changed heartbeat?

@kelunik
Copy link
Member

kelunik commented Jul 24, 2024

Hi @Konstantin-seo, sorry that I forgot to answer this long. Is this still something you're interested in?

@Konstantin-seo
Copy link
Author

Hi @Konstantin-seo, sorry that I forgot to answer this long. Is this still something you're interested in?

It would be great. Now, as a solution, I catch the break, and again, via goto, start listening on the socket

@kelunik
Copy link
Member

kelunik commented Jul 24, 2024

Is the server also something you control and have access to? Is that server also AMPHP based?

@Konstantin-seo
Copy link
Author

Is the server also something you control and have access to? Is that server also AMPHP based?

No

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

No branches or pull requests

2 participants