修正为时间戳
This commit is contained in:
parent
11a8dbf4f3
commit
74b6ae073d
13
src/chat.js
13
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)
|
||||
}
|
||||
}
|
||||
}),
|
||||
|
31
src/main.js
31
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
|
||||
|
Loading…
Reference in New Issue
Block a user