把发消息写入数据库的时间都交给数据库来创建
This commit is contained in:
+73
-28
@@ -129,21 +129,21 @@ function handleSendMessage(data, ws) {
|
|||||||
`${senderName} → ${receiverName}:${content}`
|
`${senderName} → ${receiverName}:${content}`
|
||||||
);
|
);
|
||||||
|
|
||||||
const sql = `
|
const insertSql = `
|
||||||
INSERT INTO messages
|
INSERT INTO messages
|
||||||
(message_id, sender_name, receiver_name, content)
|
(message_id, sender_name, receiver_name, content)
|
||||||
VALUES (?, ?, ?, ?)
|
VALUES (?, ?, ?, ?)
|
||||||
`;
|
`;
|
||||||
db.query(sql, [
|
db.query(insertSql, [
|
||||||
messageId,
|
messageId,
|
||||||
senderName,
|
senderName,
|
||||||
receiverName,
|
receiverName,
|
||||||
content
|
content
|
||||||
], (error, result) => {
|
], (error, result) => {
|
||||||
|
const senderWs = getConnection(senderName);
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
||||||
const senderWs = getConnection(senderName);
|
|
||||||
if (senderWs) {
|
if (senderWs) {
|
||||||
senderWs.send(
|
senderWs.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
@@ -156,34 +156,79 @@ function handleSendMessage(data, ws) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 数据库确认写入后,才确认发送并推送给接收者
|
// 插入新消息之后,用messageID查询这条新消息的时间点(为了使用数据库插入时创建的时间点,不能用前端自己生成的)
|
||||||
const senderWs = getConnection(senderName);
|
const timeSql = `
|
||||||
if (senderWs) {
|
SELECT created_at
|
||||||
senderWs.send(
|
FROM messages
|
||||||
|
WHERE message_id = ?
|
||||||
|
`;
|
||||||
|
db.query(timeSql, [messageId], (error, results) => {
|
||||||
|
if (error) {
|
||||||
|
console.error("查询消息失败:", error);
|
||||||
|
|
||||||
|
if (senderWs) {
|
||||||
|
senderWs.send(
|
||||||
|
JSON.stringify({
|
||||||
|
type: "sendError",
|
||||||
|
messageId: messageId,
|
||||||
|
message: "获取消息失败"
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (results.length === 0) {
|
||||||
|
if (senderWs) {
|
||||||
|
senderWs.send(
|
||||||
|
JSON.stringify({
|
||||||
|
type: "sendError",
|
||||||
|
messageId: messageId,
|
||||||
|
message: "获取消息失败"
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const createdAt = results[0].created_at;
|
||||||
|
|
||||||
|
// 数据库确认写入后,才把成功写入的消息推送给发送者
|
||||||
|
if (senderWs) {
|
||||||
|
senderWs.send(
|
||||||
|
JSON.stringify({
|
||||||
|
type: "sendSuccess",
|
||||||
|
message: {
|
||||||
|
messageId,
|
||||||
|
senderName,
|
||||||
|
receiverName,
|
||||||
|
content,
|
||||||
|
createdAt
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 把写入的新消息推送给接收者,如果接收者不在线,就不用发送websocket消息给他了,但数据库里是有的
|
||||||
|
const receiverWs = getConnection(receiverName);
|
||||||
|
if (!receiverWs) {
|
||||||
|
console.log(`${receiverName} 当前不在线`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
receiverWs.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
type: "sendSuccess",
|
type: "newMessage",
|
||||||
messageId: messageId
|
message: {
|
||||||
|
messageId,
|
||||||
|
senderName,
|
||||||
|
receiverName,
|
||||||
|
content,
|
||||||
|
createdAt
|
||||||
|
},
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
});
|
||||||
|
|
||||||
const receiverWs = getConnection(receiverName);
|
|
||||||
if (!receiverWs) {
|
|
||||||
console.log(`${receiverName} 当前不在线`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
receiverWs.send(
|
|
||||||
JSON.stringify({
|
|
||||||
type: "newMessage",
|
|
||||||
message: {
|
|
||||||
messageId,
|
|
||||||
senderName,
|
|
||||||
receiverName,
|
|
||||||
content
|
|
||||||
},
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user