Compare commits
No commits in common. "212adad2afba718f13cb621dd74f7bc2544108b0" and "77fd29efb4929f5fd85e346739baff30ddf2fb0b" have entirely different histories.
212adad2af
...
77fd29efb4
43
src/chat.js
43
src/chat.js
@ -101,10 +101,10 @@ export default class Chat {
|
|||||||
color: #888;
|
color: #888;
|
||||||
}`
|
}`
|
||||||
}, 'style'))
|
}, 'style'))
|
||||||
this.从本地载入消息()
|
this.载入消息()
|
||||||
this.挂载全局快捷键()
|
this.挂载全局快捷键()
|
||||||
}
|
}
|
||||||
async 挂载全局快捷键() {
|
挂载全局快捷键() {
|
||||||
document.addEventListener('keydown', event => {
|
document.addEventListener('keydown', event => {
|
||||||
if (event.key === 'Enter' || event.key === '/') {
|
if (event.key === 'Enter' || event.key === '/') {
|
||||||
this.element.style.display = 'block'
|
this.element.style.display = 'block'
|
||||||
@ -112,28 +112,6 @@ 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) {
|
answer(data) {
|
||||||
const { name, text, time, type } = data
|
const { name, text, time, type } = data
|
||||||
@ -192,15 +170,24 @@ export default class Chat {
|
|||||||
存储消息(data) {
|
存储消息(data) {
|
||||||
set(data.id, data, this.store)
|
set(data.id, data, this.store)
|
||||||
}
|
}
|
||||||
|
载入消息() {
|
||||||
|
values(this.store).then(items => {
|
||||||
|
items.map(item => {
|
||||||
|
item.timestamp = new Date(item.time).getTime()
|
||||||
|
return item
|
||||||
|
}).sort((a, b) => a.timestamp - b.timestamp).forEach(item => {
|
||||||
|
this.添加元素(item)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
发送消息(text) {
|
发送消息(text) {
|
||||||
const name = '我'
|
const name = '我'
|
||||||
const id = window.crypto.randomUUID()
|
const id = window.crypto.randomUUID()
|
||||||
const time = new Date().toLocaleString()
|
const time = new Date().toLocaleString()
|
||||||
const type = 'text'
|
const type = 'text'
|
||||||
const data = { id, name, text, time, type }
|
this.添加元素({ id, name, text, time, type })
|
||||||
this.添加元素(data)
|
this.存储消息({ id, name, text, time, type })
|
||||||
this.存储消息(data)
|
this.send({ id, name, text, time, type })
|
||||||
this.send(data)
|
|
||||||
}
|
}
|
||||||
收到消息(data) {
|
收到消息(data) {
|
||||||
console.log('收到消息', data)
|
console.log('收到消息', data)
|
||||||
|
15
src/main.js
15
src/main.js
@ -150,10 +150,6 @@ const chat = new Chat({
|
|||||||
clientList.setChannel('chat', {
|
clientList.setChannel('chat', {
|
||||||
onopen: async event => {
|
onopen: async event => {
|
||||||
//console.debug('打开信道', event.target.label)
|
//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) => {
|
onmessage: async (event, client) => {
|
||||||
const data = JSON.parse(event.data)
|
const data = JSON.parse(event.data)
|
||||||
@ -167,17 +163,6 @@ clientList.setChannel('chat', {
|
|||||||
chat.answer(data)
|
chat.answer(data)
|
||||||
return
|
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)
|
console.log('未知类型:', data.type)
|
||||||
},
|
},
|
||||||
onclose: event => {
|
onclose: event => {
|
||||||
|
Loading…
Reference in New Issue
Block a user