From 74b6ae073d2449fdf3483d1e08dc7d79b55574d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Sat, 21 Oct 2023 18:40:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=B8=BA=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=88=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat.js | 13 ++++--------- src/main.js | 31 +++++++++++-------------------- 2 files changed, 15 insertions(+), 29 deletions(-) diff --git a/src/chat.js b/src/chat.js index 023e4b7..2b26265 100644 --- a/src/chat.js +++ b/src/chat.js @@ -12,7 +12,7 @@ export default class Chat { position: 'fixed', bottom: 0, left: '50%', maxWidth: '64rem', width: '100%', transform: 'translate(-50%, 0)', - display: 'none', + display: 'block', transition: 'all .15s', }, children: [ @@ -71,15 +71,10 @@ export default class Chat { // 清理本地存储 values(this.store).then(list => list.forEach(item => { const t = new Date(item.time).getTime() - console.log('清理', item.id, t, timestamp) - if (t < timestamp) { - del(item.id, this.store) - console.log('清理成功', item.id) - } + if (t < timestamp) del(item.id, this.store) })) - const time = new Date().toLocaleString() - console.log('清屏', `store-chat-${name}`, time) - localStorage.setItem(`store-chat-${name}`, time) + console.log('清屏', `store-chat-${name}`, timestamp) + localStorage.setItem(`store-chat-${name}`, timestamp) } } }), diff --git a/src/main.js b/src/main.js index 59f27f1..59e6717 100644 --- a/src/main.js +++ b/src/main.js @@ -149,19 +149,9 @@ const chat = new Chat({ // 与每个客户端都建立聊天信道 clientList.setChannel('chat', { onopen: async event => { - // 请求指定范围内的消息列表(转换成时间戳) - const last = localStorage.getItem('store-chat-default') - const lastTimestamp = new Date(last).getTime() - console.log('上次清理消息时间:', last, lastTimestamp) - // 如果超过一周, 则只拉取一周内的消息 - if (lastTimestamp > new Date().getTime() - 1000 * 60 * 60 * 24 * 7) { - const start = new Date().getTime() - 1000 * 60 * 60 * 24 * 7 - const end = new Date().getTime() - clientList.send('chat', JSON.stringify({ type: 'pull', start, end })) - return - } - // 否则拉取时间戳范围内的消息 - clientList.send('chat', JSON.stringify({ type: 'pull', start: lastTimestamp, end: new Date().getTime() })) + const start = localStorage.getItem('store-chat-default') || 0 + const end = new Date().getTime() + clientList.send('chat', JSON.stringify({ type: 'pull', start, end })) }, onmessage: async (event, client) => { const data = JSON.parse(event.data) @@ -177,19 +167,20 @@ clientList.setChannel('chat', { } if (data.type === 'pull') { console.log(client.name, '请求拉取消息:', data) - const list = await chat.筛选指定范围的消息({ start: data.start, end: data.end }) + const list = (await chat.筛选指定范围的消息({ start: data.start, end: data.end })).map(item => { + return { timestamp: new Date(item.time).getTime(), ...item } + }) clientList.sendto(client.id, 'chat', JSON.stringify({ type: 'list', list })) return } if (data.type === 'list') { console.log(client.name, '发来消息列表:', data) - const last = localStorage.getItem('store-chat-default') - const lastTimestamp = new Date(last).getTime() - data.list.map(item => ({ timestamp: new Date(item.time).getTime(), ...item })).filter(item => { - const timestamp = new Date(item.time).getTime() - return timestamp >= lastTimestamp + const timestamp = localStorage.getItem('store-chat-default') || 0 + data.list.filter(item => { + return item.timestamp < timestamp || item.timestamp > new Date().getTime() }).forEach(item => { - console.log('消息不符合要求的时间区间', last, new Date().getTime(), item) + console.log('要求的消息时段:', timestamp) + console.log('错误的消息时段:', item) }) chat.合并消息列表(data.list) return