diff --git a/chat.py b/chat.py index 31b5a54720b2cdca92ecbbd490860098f48f46b7..145c120df53aed39698f542f84c65460c6764119 100644 --- a/chat.py +++ b/chat.py @@ -44,16 +44,16 @@ async def echo(websocket): #根据api-token查找房间号 room_id = name[websocket]['room_id'] #历史记录数量 - if "historynum" in event["type"]: + if "history_num" in event["type"]: r = { - "type": "historynum", - "content": "{}", + "type": "history_num", + "content": "{}" } if room_id in history : h = len(history[room_id]) else : h = 0 - r['content'] = r['content'].format(h) + r["content"] = r["content"].format(h) message = json.dumps(r,ensure_ascii=False) await websocket.send(message) #历史记录 diff --git a/websockets/chat.py b/websockets/chat.py index d558d3a901e983b68df261460f11b480ac110207..49933362ba68b283022bb5eb470d91eaa2879775 100644 --- a/websockets/chat.py +++ b/websockets/chat.py @@ -42,12 +42,27 @@ async def echo(websocket): event = json.loads(message) #根据api-token查找房间号 room_id = name[websocket]['room_id'] + #历史记录数量 + if "history_num" in event["type"]: + r = { + "type": "history_num", + "content": "{}" + } + if room_id in history : + h = len(history[room_id]) + else : + h = 0 + r["content"] = r["content"].format(h) + message = json.dumps(r,ensure_ascii=False) + await websocket.send(message) #历史记录 if "history" in event["type"]: start = int(event['page']) end = int(event['page_num']) + num = int(event['num']) if room_id in history : - h = history[room_id][start*end - end:start*end] + n = history[room_id][:num] + h = n[start*end - end:start*end] else : h = [] r = {'type':'history','content':h}