From 212adad2afba718f13cb621dd74f7bc2544108b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Sat, 21 Oct 2023 16:17:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat.js | 32 ++++++++++++++++++++++++-------- src/main.js | 15 +++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/chat.js b/src/chat.js index 8fc8144..38e7cb8 100644 --- a/src/chat.js +++ b/src/chat.js @@ -101,10 +101,10 @@ export default class Chat { color: #888; }` }, 'style')) - this.载入消息() + this.从本地载入消息() this.挂载全局快捷键() } - 挂载全局快捷键() { + async 挂载全局快捷键() { document.addEventListener('keydown', event => { if (event.key === 'Enter' || event.key === '/') { this.element.style.display = 'block' @@ -112,6 +112,28 @@ export default class Chat { } }) } + async 从本地载入消息() { + const data = await values(this.store) + data.map(item => ({ timestamp: new Date(item.time).getTime(), ...item })).sort((a, b) => a.timestamp - b.timestamp).forEach(item => { + this.添加元素(item) + }) + } + async 筛选指定范围的消息({ start, end }) { + const data = await values(this.store) + return data.map(item => ({ timestamp: new Date(item.time).getTime(), ...item })).filter(item => { + const timestamp = new Date(item.time).getTime() + return timestamp >= start && timestamp <= end + }) + } + async 合并消息列表(list) { + // 检查本地不存在的id,存储消息 + const data = await values(this.store) + const ids = data.map(item => item.id) + list.filter(item => !ids.includes(item.id)).forEach(item => { + this.添加元素(item) + this.存储消息(item) + }) + } // 收到应答(对方确认消息已被接收) answer(data) { const { name, text, time, type } = data @@ -170,12 +192,6 @@ export default class Chat { 存储消息(data) { set(data.id, data, this.store) } - async 载入消息() { - const data = await values(this.store) - data.map(item => ({ timestamp: new Date(item.time).getTime(), ...item })).sort((a, b) => a.timestamp - b.timestamp).forEach(item => { - this.添加元素(item) - }) - } 发送消息(text) { const name = '我' const id = window.crypto.randomUUID() diff --git a/src/main.js b/src/main.js index 5df41c4..cf68c07 100644 --- a/src/main.js +++ b/src/main.js @@ -150,6 +150,10 @@ const chat = new Chat({ clientList.setChannel('chat', { onopen: async event => { //console.debug('打开信道', event.target.label) + // 请求指定范围内的消息列表 + const start = new Date().getTime() - 1000 * 60 * 60 * 24 * 7 // 一周内的消息 + const end = new Date().getTime() + clientList.send('chat', JSON.stringify({ type: 'pull', start, end })) }, onmessage: async (event, client) => { const data = JSON.parse(event.data) @@ -163,6 +167,17 @@ clientList.setChannel('chat', { chat.answer(data) return } + if (data.type === 'pull') { + console.log(client.name, '请求拉取消息:', data) + const list = await chat.筛选指定范围的消息({ start: data.start, end: data.end }) + clientList.sendto(client.id, 'chat', JSON.stringify({ type: 'list', list })) + return + } + if (data.type === 'list') { + console.log(client.name, '发来消息列表:', data) + chat.合并消息列表(data.list) + return + } console.log('未知类型:', data.type) }, onclose: event => {