在实现taro 的聊天室功能时,由于需求仅仅需要实现聊天,在线人数不多,使用频率较低,不想架构的太复杂,没有使用到websocket技术,,就想利用定时器定时获取数据再加载,服务端仅仅用传统接口来实现,数据存储在mysql,发现测试效果还是不错的。以下附上部分代码和思路
//重新刷新数据
refreshChatData() {
console.log('refreshChatData-------')
this.loadChatData()
}
//加载历史数据
async loadChatData() {
const currentChatKey = this.state.chatKey
if (currentChatKey == '') {
Taro.showToast({
title: 'chatKey不能为空'
})
return
}
const payloadData = {
chat_key: currentChatKey,
user_id: this.paramUserId,
access_token: ''
}
const oldChatData = this.state.chatData;
const newChatData = await apiFetch({ url: '/api/C