DEBUG: 修正请求同步次数
This commit is contained in:
parent
74b6ae073d
commit
2808da61a0
@ -138,15 +138,14 @@ export default class Chat {
|
||||
})
|
||||
}
|
||||
async 筛选指定范围的消息({ start, end }) {
|
||||
console.log('筛选指定范围的消息', 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
|
||||
})
|
||||
}
|
||||
// 检查本地不存在的id,存储消息
|
||||
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 => {
|
||||
|
15
src/main.js
15
src/main.js
@ -148,10 +148,10 @@ const chat = new Chat({
|
||||
|
||||
// 与每个客户端都建立聊天信道
|
||||
clientList.setChannel('chat', {
|
||||
onopen: async event => {
|
||||
onopen: async (event, client) => {
|
||||
const start = localStorage.getItem('store-chat-default') || 0
|
||||
const end = new Date().getTime()
|
||||
clientList.send('chat', JSON.stringify({ type: 'pull', start, end }))
|
||||
clientList.sendto(client.id, 'chat', JSON.stringify({ type: 'pull', start, end }))
|
||||
},
|
||||
onmessage: async (event, client) => {
|
||||
const data = JSON.parse(event.data)
|
||||
@ -166,22 +166,15 @@ clientList.setChannel('chat', {
|
||||
return
|
||||
}
|
||||
if (data.type === 'pull') {
|
||||
console.log(client.name, '请求拉取消息:', data)
|
||||
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 }))
|
||||
console.log(client.name, `拉取了 ${list.length} 条消息`)
|
||||
return
|
||||
}
|
||||
if (data.type === 'list') {
|
||||
console.log(client.name, '发来消息列表:', data)
|
||||
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('要求的消息时段:', timestamp)
|
||||
console.log('错误的消息时段:', item)
|
||||
})
|
||||
console.log(client.name, `同步来 ${data.list.length} 条消息`)
|
||||
chat.合并消息列表(data.list)
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user