Skip to content

Commit 975adf3

Browse files
committed
Refactor message handling: improve session management and add timeout configuration
1 parent 5763e06 commit 975adf3

3 files changed

Lines changed: 35 additions & 29 deletions

File tree

StealthIM/apis/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
from . import user
33
from . import util
44
from . import group
5-
from . import message
5+
from . import message

StealthIM/apis/message.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
from .common import request, NoValResult, Result
99
from .. import logger
1010

11+
timeout_for_sse = aiohttp.ClientTimeout(
12+
total=None, # 整体无超时
13+
sock_connect=None, # 连接无超时
14+
sock_read=None # 读取无超时
15+
)
16+
1117

1218
class MessageType(enum.Enum):
1319
Text = 0
@@ -105,29 +111,31 @@ async def get_message(
105111
"Authorization": f"Bearer {session}",
106112
}
107113

108-
async with aiohttp.ClientSession(headers=headers) as session:
109-
async with session.get(api_address) as response:
110-
if response.status != 200:
111-
raise RuntimeError(f"Request failed with status: {response.status}")
112-
113-
async for line in response.content:
114-
# 解码成字符串
115-
line = line.decode('utf-8').strip()
116-
if line.startswith('data:'):
117-
# 提取消息内容
118-
message = line[len('data:'):].strip()
119-
120-
data = json.loads(message)
121-
logger.debug(f"Response data: {data}")
122-
if data["result"]["code"] != 800:
123-
raise RuntimeError(f"Request failed with code: {data['result']['code']}")
124-
for msg in data["msg"]:
125-
yield Message(
126-
groupid=msg["groupid"],
127-
msg=msg["msg"],
128-
msgid=msg["msgid"],
129-
time=msg["time"],
130-
type=MessageType(msg["type"]),
131-
username=msg["username"],
132-
hash=msg.get("hash", None),
133-
)
114+
async with (
115+
aiohttp.ClientSession(headers=headers, timeout=timeout_for_sse) as session,
116+
session.get(api_address) as response
117+
):
118+
if response.status != 200:
119+
raise RuntimeError(f"Request failed with status: {response.status}")
120+
121+
async for line in response.content:
122+
# 解码成字符串
123+
line = line.decode('utf-8').strip()
124+
if line.startswith('data:'):
125+
# 提取消息内容
126+
message = line[len('data:'):].strip()
127+
128+
data = json.loads(message)
129+
logger.debug(f"Response data: {data}")
130+
if data["result"]["code"] != 800:
131+
raise RuntimeError(f"Request failed with code: {data['result']['code']}")
132+
for msg in data["msg"]:
133+
yield Message(
134+
groupid=msg["groupid"],
135+
msg=msg["msg"],
136+
msgid=msg["msgid"],
137+
time=msg["time"],
138+
type=MessageType(msg["type"]),
139+
username=msg["username"],
140+
hash=msg.get("hash", None),
141+
)

StealthIM/group.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ async def join(self, password: str) -> JoinGroupResult:
3333
Join a Group.
3434
3535
Args:
36-
user (User): The user to join.
3736
password (str): The password.
3837
3938
Returns:
@@ -44,7 +43,6 @@ async def join(self, password: str) -> JoinGroupResult:
4443
"""
4544
return await StealthIM.apis.group.join_group(self.user.server.url, self.user.session, self.group_id, password)
4645

47-
4846
async def get_members(self) -> GroupInfoResult:
4947
"""
5048
Get members of the group.

0 commit comments

Comments
 (0)