]> git.localhorst.tv Git - alttp.git/commitdiff
actively ping IRC connection
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 1 Jan 2024 13:14:39 +0000 (14:14 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 1 Jan 2024 13:14:39 +0000 (14:14 +0100)
app/TwitchBot/IRCMessage.php
app/TwitchBot/TwitchBot.php

index c730ef7d0bfa962126417f7b513e7adc46984b9b..d127f9f5d57425e850512640d6ae470c2f0c1bc3 100644 (file)
@@ -139,6 +139,13 @@ class IRCMessage {
                return $msg;
        }
 
+       public static function ping($token = 'localhorsttv') {
+               $msg = new IRCMessage();
+               $msg->command = 'PING';
+               $msg->params[] = $token;
+               return $msg;
+       }
+
        public static function privmsg($target, $message) {
                $msg = new IRCMessage();
                $msg->command = 'PRIVMSG';
@@ -169,6 +176,10 @@ class IRCMessage {
                return $this->command == 'PING';
        }
 
+       public function isPong() {
+               return $this->command == 'PONG';
+       }
+
        public function isPrivMsg() {
                return $this->command == 'PRIVMSG';
        }
index ddc51fe046b2e68d448a18b04cc272ab8702a8c3..a45de87fcce69a36b32e06cdb2fc567b222144a0 100644 (file)
@@ -26,6 +26,7 @@ class TwitchBot {
                $this->connector = new Connector();
                $this->connect();
                $this->listenCommands();
+               $this->startPinger();
        }
 
        public function getLogger() {
@@ -86,8 +87,8 @@ class TwitchBot {
                $this->ready = false;
                $this->logger->info('websocket connection closed: '.$reason.' ['.$op.']');
                if (!$this->shutting_down) {
-                       $this->logger->info('reconnecting in 10 seconds');
-                       Loop::addTimer(10, [$this, 'connect']);
+                       $this->logger->info('reconnecting in 5 seconds');
+                       Loop::addTimer(5, [$this, 'connect']);
                }
        }
 
@@ -97,6 +98,7 @@ class TwitchBot {
 
 
        public function handleIRCMessage(IRCMessage $msg) {
+               $this->last_contact = time();
                if ($msg->isPrivMsg()) {
                        $this->handlePrivMsg($msg);
                        return;
@@ -105,6 +107,9 @@ class TwitchBot {
                        $this->sendIRCMessage($msg->makePong());
                        return;
                }
+               if ($msg->isPong()) {
+                       return;
+               }
                if ($msg->isNotice() && $msg->getText() == 'Login authentication failed') {
                        $this->logger->notice('login failed, refreshing access token');
                        $this->token->refresh();
@@ -171,13 +176,24 @@ class TwitchBot {
                                }
                        }
                });
+       }
 
+       private function startPinger() {
+               $this->getLoop()->addPeriodicTimer(15, function () {
+                       if (!$this->ready) return;
+                       if (time() - $this->last_contact < 60) return;
+                       try {
+                               $this->sendIRCMessage(IRCMessage::ping());
+                       } catch (\Exception $e) {
+                       }
+               });
        }
 
        public function sendIRCMessage(IRCMessage $msg) {
                $irc_message = $msg->encode();
                $this->logger->info('sending IRC message '.$irc_message);
                $this->ws->send($irc_message);
+               $this->last_contact = time();
        }
 
 
@@ -190,6 +206,8 @@ class TwitchBot {
        private $ready = false;
        private $shutting_down = false;
 
+       private $last_contact;
+
 }
 
 ?>