add check
This commit is contained in:
+8
-37
@@ -85,16 +85,15 @@ class TwitchIRCClient:
|
||||
|
||||
decoded = line.decode('utf-8', errors='ignore').strip()
|
||||
|
||||
# PING от Twitch — отвечаем СРАЗУ, возвращаем None чтобы не путать с нашим PONG
|
||||
# PING от Twitch — отвечаем СРАЗУ и возвращаем строку (обновит last_message_time)
|
||||
if decoded.startswith('PING'):
|
||||
pong_msg = f"PONG {decoded.split()[1]}"
|
||||
self._writer.write(f"{pong_msg}\r\n".encode())
|
||||
self._writer.write(b"PONG :tmi.twitch.tv\r\n")
|
||||
try:
|
||||
await self._writer.drain()
|
||||
except (ConnectionResetError, BrokenPipeError, OSError) as e:
|
||||
logger.warning(f"Failed to send PONG: {e}")
|
||||
logger.debug(f"🏓 {pong_msg}")
|
||||
return None # не пробрасываем в основной цикл
|
||||
logger.debug("🏓 PONG :tmi.twitch.tv")
|
||||
return decoded # возвращаем чтобы основной цикл обновил last_message_time
|
||||
|
||||
return decoded
|
||||
|
||||
@@ -176,11 +175,6 @@ class TwitchIRCClient:
|
||||
|
||||
start_time = datetime.now()
|
||||
last_message_time = datetime.now()
|
||||
last_ping_time = datetime.now()
|
||||
ping_interval = 120 # пинговать каждые 2 минуты
|
||||
pong_timeout = 30 # ждать PONG 30 сек, иначе реконнект
|
||||
waiting_pong = False
|
||||
ping_sent_at: Optional[float] = None
|
||||
drain_errors = 0
|
||||
|
||||
# Если задача стартует на паузе — ждём resume до подключения к IRC
|
||||
@@ -205,40 +199,19 @@ class TwitchIRCClient:
|
||||
logger.info(f"▶️ #{self.channel} resumed, reconnecting")
|
||||
await self.connect()
|
||||
last_message_time = datetime.now()
|
||||
last_ping_time = datetime.now()
|
||||
waiting_pong = False
|
||||
continue
|
||||
|
||||
# Нет соединения — ждём перед повтором
|
||||
if not self._connected:
|
||||
await asyncio.sleep(5)
|
||||
await self.connect()
|
||||
last_ping_time = datetime.now()
|
||||
waiting_pong = False
|
||||
last_message_time = datetime.now()
|
||||
continue
|
||||
|
||||
now = datetime.now()
|
||||
|
||||
# === PONG не пришёл вовремя — соединение мёртвое, реконнект ===
|
||||
if waiting_pong and ping_sent_at and (now.timestamp() - ping_sent_at) > pong_timeout:
|
||||
logger.warning(f"🔌 #{self.channel} PONG timeout, reconnecting...")
|
||||
waiting_pong = False
|
||||
self._connected = False
|
||||
continue
|
||||
|
||||
# === Отправляем PING каждые ping_interval секунд ===
|
||||
if not waiting_pong and (now - last_ping_time).seconds >= ping_interval:
|
||||
try:
|
||||
await self._send("PING :tmi.twitch.tv")
|
||||
ping_sent_at = now.timestamp()
|
||||
waiting_pong = True
|
||||
last_ping_time = now
|
||||
except Exception:
|
||||
self._connected = False
|
||||
continue
|
||||
|
||||
# === Реконнект если вообще нет активности > 5 минут (failsafe) ===
|
||||
if (now - last_message_time).seconds > 300:
|
||||
# === Реконнект если нет активности > 6 минут (Twitch PING каждые ~5 мин) ===
|
||||
if (now - last_message_time).total_seconds() > 360:
|
||||
logger.info("🔄 No activity 5min, reconnecting...")
|
||||
try:
|
||||
await self.disconnect()
|
||||
@@ -272,10 +245,8 @@ class TwitchIRCClient:
|
||||
await asyncio.sleep(0.5)
|
||||
continue
|
||||
|
||||
# Twitch отвечает: ":tmi.twitch.tv PONG tmi.twitch.tv :..." — не startswith
|
||||
if 'PONG' in line:
|
||||
if 'PONG' in line or line.startswith('PING'):
|
||||
last_message_time = datetime.now()
|
||||
waiting_pong = False
|
||||
continue
|
||||
|
||||
msg = self._parse_message(line)
|
||||
|
||||
Reference in New Issue
Block a user