我正在尝试建立一个聊天应用程序(一对一)使用node.js,express,mysql和socket为后端和flutter为客户端…问题是当socket没有连接或超时。。。我想用redis来处理这个问题…所以当用户a想给用户b发送消息,但是socket断开时,我想用redis来存储聊天,然后在socket连接后再发送回来。。。我试着搜索一些关于它的资源或文章,但很难找到如何使用redis和socket进行聊天的应用程序。。。有人知道如何使用redis编写socket的资源、文档或代码吗?下面是我的部分代码后,我遵循一些教程
io.on('connection', socket => {
id = socket.handshake.query.id
socket.join(id)
socket.on('sendmessage', x => {
receiver = x.receiver
sender = x.sender
message = x.message
var d = new Date(),
dformat = [d.getFullYear(),
d.getMonth() + 1,
d.getDate()].join('-') + ' ' +
[d.getHours(),
d.getMinutes(),
d.getSeconds()].join(':');
connection.query("INSERT INTO chat (sender, receiver, message, date) VALUES ('" + sender + "', '" + receiver + "', '" + message + "', '" + dformat + "')", function (error, result) {
});
socket.in(receiver).emit('receivemessage', {
'message': message,
'sender': sender,
'receiver': receiver,
})
connection.query("SELECT * FROM chat WHERE (sender = '" + sender + "' AND receiver = '" + receiver + "') OR (sender = '" + receiver + "' AND receiver = '" + sender + "') ORDER BY date", function (err, messages) {
});
})
});
暂无答案!
目前还没有任何答案,快来回答吧!