From ccd8aa7874d300849602fa07fedf97e09fde1a11 Mon Sep 17 00:00:00 2001 From: Yuanzhang Hu Date: Sat, 11 May 2024 09:21:51 -0400 Subject: [PATCH] every 1 hour, we get wss_url again --- .gitignore | 1 + chatgpt/ChatService.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index f08d9d8..6e49c93 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .env /.idea/ +*.pyc diff --git a/chatgpt/ChatService.py b/chatgpt/ChatService.py index 3f2a17c..eea7b3a 100644 --- a/chatgpt/ChatService.py +++ b/chatgpt/ChatService.py @@ -2,6 +2,7 @@ import json import random import types import uuid +import time import websockets from fastapi import HTTPException @@ -22,6 +23,7 @@ class ChatService: self.ws = None self.wss_url = None self.wss_mode = False + self.lasttime_get_wss_url = 0 self.user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0" self.access_token = access_token self.chat_token = "gAAAAAB" @@ -226,8 +228,9 @@ class ChatService: wss_r = None try: if self.wss_mode: - if not self.wss_url: + if not self.wss_url or time.time() > 3600 + self.lasttime_get_wss_url: self.wss_url = await self.get_wss_url() # first wss_url + self.lasttime_get_wss_url = time.time() # websocket connection has to be established before posting conversation to avoid message loss. if self.wss_url: self.ws = await websockets.connect(self.wss_url, ping_interval=None, subprotocols=subprotocols) @@ -257,6 +260,7 @@ class ChatService: rtext = await r.atext() resp = json.loads(rtext) self.wss_url = resp.get('wss_url') # used for next wss_url + self.lasttime_get_wss_url = time.time() logger.info(f"next wss_url: {self.wss_url}") if wss_r: try: