修正为时间戳
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%',
|
position: 'fixed', bottom: 0, left: '50%',
|
||||||
maxWidth: '64rem', width: '100%',
|
maxWidth: '64rem', width: '100%',
|
||||||
transform: 'translate(-50%, 0)',
|
transform: 'translate(-50%, 0)',
|
||||||
display: 'none',
|
display: 'block',
|
||||||
transition: 'all .15s',
|
transition: 'all .15s',
|
||||||
},
|
},
|
||||||
children: [
|
children: [
|
||||||
@ -71,15 +71,10 @@ export default class Chat {
|
|||||||
// 清理本地存储
|
// 清理本地存储
|
||||||
values(this.store).then(list => list.forEach(item => {
|
values(this.store).then(list => list.forEach(item => {
|
||||||
const t = new Date(item.time).getTime()
|
const t = new Date(item.time).getTime()
|
||||||
console.log('清理', item.id, t, timestamp)
|
if (t < timestamp) del(item.id, this.store)
|
||||||
if (t < timestamp) {
|
|
||||||
del(item.id, this.store)
|
|
||||||
console.log('清理成功', item.id)
|
|
||||||
}
|
|
||||||
}))
|
}))
|
||||||
const time = new Date().toLocaleString()
|
console.log('清屏', `store-chat-${name}`, timestamp)
|
||||||
console.log('清屏', `store-chat-${name}`, time)
|
localStorage.setItem(`store-chat-${name}`, timestamp)
|
||||||
localStorage.setItem(`store-chat-${name}`, time)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
31
src/main.js
31
src/main.js
@ -149,19 +149,9 @@ const chat = new Chat({
|
|||||||
// 与每个客户端都建立聊天信道
|
// 与每个客户端都建立聊天信道
|
||||||
clientList.setChannel('chat', {
|
clientList.setChannel('chat', {
|
||||||
onopen: async event => {
|
onopen: async event => {
|
||||||
// 请求指定范围内的消息列表(转换成时间戳)
|
const start = localStorage.getItem('store-chat-default') || 0
|
||||||
const last = localStorage.getItem('store-chat-default')
|
const end = new Date().getTime()
|
||||||
const lastTimestamp = new Date(last).getTime()
|
clientList.send('chat', JSON.stringify({ type: 'pull', start, end }))
|
||||||
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() }))
|
|
||||||
},
|
},
|
||||||
onmessage: async (event, client) => {
|
onmessage: async (event, client) => {
|
||||||
const data = JSON.parse(event.data)
|
const data = JSON.parse(event.data)
|
||||||
@ -177,19 +167,20 @@ clientList.setChannel('chat', {
|
|||||||
}
|
}
|
||||||
if (data.type === 'pull') {
|
if (data.type === 'pull') {
|
||||||
console.log(client.name, '请求拉取消息:', data)
|
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 }))
|
clientList.sendto(client.id, 'chat', JSON.stringify({ type: 'list', list }))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (data.type === 'list') {
|
if (data.type === 'list') {
|
||||||
console.log(client.name, '发来消息列表:', data)
|
console.log(client.name, '发来消息列表:', data)
|
||||||
const last = localStorage.getItem('store-chat-default')
|
const timestamp = localStorage.getItem('store-chat-default') || 0
|
||||||
const lastTimestamp = new Date(last).getTime()
|
data.list.filter(item => {
|
||||||
data.list.map(item => ({ timestamp: new Date(item.time).getTime(), ...item })).filter(item => {
|
return item.timestamp < timestamp || item.timestamp > new Date().getTime()
|
||||||
const timestamp = new Date(item.time).getTime()
|
|
||||||
return timestamp >= lastTimestamp
|
|
||||||
}).forEach(item => {
|
}).forEach(item => {
|
||||||
console.log('消息不符合要求的时间区间', last, new Date().getTime(), item)
|
console.log('要求的消息时段:', timestamp)
|
||||||
|
console.log('错误的消息时段:', item)
|
||||||
})
|
})
|
||||||
chat.合并消息列表(data.list)
|
chat.合并消息列表(data.list)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user