把发消息写入数据库的时间都交给数据库来创建,只读取
This commit is contained in:
@@ -296,7 +296,6 @@ export default {
|
||||
|
||||
created() {
|
||||
this.getCurrentUser();
|
||||
this.getRecentMessageList();
|
||||
this.createWebsocket();
|
||||
},
|
||||
|
||||
@@ -324,6 +323,7 @@ export default {
|
||||
getUserInfo()
|
||||
.then((response) => {
|
||||
this.currentUser = response.data;
|
||||
this.getRecentMessageList();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
@@ -435,31 +435,10 @@ export default {
|
||||
senderName: this.currentUser.username,
|
||||
receiverName: this.currentContact.contactName,
|
||||
content: content,
|
||||
createdAt: new Date(),
|
||||
};
|
||||
|
||||
try {
|
||||
sendWebSocketMessage(newMessage);
|
||||
this.contactMessages.push({
|
||||
...newMessage,
|
||||
from: "me",
|
||||
createdAt: newMessage.createdAt,
|
||||
time: newMessage.createdAt,
|
||||
});
|
||||
|
||||
// 更新最近消息列表
|
||||
this.updateRecentChat(
|
||||
"send",
|
||||
this.currentContact.contactName,
|
||||
newMessage
|
||||
);
|
||||
console.log("contactMessages:" + this.contactMessages);
|
||||
|
||||
// 清空输入框
|
||||
this.messageText = "";
|
||||
this.$nextTick(() => {
|
||||
this.scrollToBottom();
|
||||
});
|
||||
} catch (error) {
|
||||
// 清空输入框
|
||||
this.messageText = "";
|
||||
@@ -539,7 +518,7 @@ export default {
|
||||
// 给发消息的人返回 ACK
|
||||
sendWebSocketMessage({
|
||||
type: "ack",
|
||||
messageId: message.messageId
|
||||
messageId: message.messageId,
|
||||
});
|
||||
|
||||
const currentContactName =
|
||||
@@ -566,6 +545,23 @@ export default {
|
||||
this.$nextTick(() => {
|
||||
this.scrollToBottom();
|
||||
});
|
||||
|
||||
// 发送新消息成功之后,需要把新消息显示到聊天框中(由于要拿数据库里的写入时间,所以要等websocket返回成功再刷新)
|
||||
} else if (data.type == "sendSuccess") {
|
||||
const message = data.message;
|
||||
this.contactMessages.push({
|
||||
...message,
|
||||
from: "me",
|
||||
time: message.createdAt,
|
||||
});
|
||||
|
||||
// 更新最近消息列表
|
||||
this.updateRecentChat("send", this.currentContact.contactName, message);
|
||||
// 清空输入框
|
||||
this.messageText = "";
|
||||
this.$nextTick(() => {
|
||||
this.scrollToBottom();
|
||||
});
|
||||
} else if (data.type == "sendError") {
|
||||
this.$message.error("发送失败,请重试");
|
||||
this.$nextTick(() => {
|
||||
|
||||
Reference in New Issue
Block a user