Javascript 我正试图在localhost中测试我的实时facebook聊天应用程序
为了测试应用程序,我对一个用户使用Chrome普通窗口,对另一个用户使用incognito窗口。 非套接字使用incognito窗口向用户发送数据。但是,它们在前端和后端之间发送数据,但仅针对调用它们的用户。 我使用同一台本地机器进行测试的方式是否错误 以下是一些片段: 客户端:Javascript 我正试图在localhost中测试我的实时facebook聊天应用程序,javascript,node.js,vue.js,socket.io,Javascript,Node.js,Vue.js,Socket.io,为了测试应用程序,我对一个用户使用Chrome普通窗口,对另一个用户使用incognito窗口。 非套接字使用incognito窗口向用户发送数据。但是,它们在前端和后端之间发送数据,但仅针对调用它们的用户。 我使用同一台本地机器进行测试的方式是否错误 以下是一些片段: 客户端: submit() { if (this.text) { this.socket.emit("new-message", { text: this.
submit() {
if (this.text) {
this.socket.emit("new-message", {
text: this.text,
sender: this._id,
receiver: this.userPop._id,
date: Date.now(),
seen: false,
});
const msg = {
text: this.text,
sender: this._id,
receiver: this.userPop._id,
date: Date.now(),
seen: false,
};
this.sent.push(msg);
this.text = "";
后端套接字:
socket.on("new-message", async(data)=>{
await console.log(data)
await Message.create(data)
.then(async (msg) => {
return msg.save();
})
.then(async (msg) => {
const newMsg = await msg.toObject();
console.log(newMsg);
socket.emit('fetch-message',{newMsg})
})
.catch(err=> console.log(err))
})
这不是你问题的答案。但是您不需要等待console.logs。是的,我知道,但在套接字中,直到我执行同步等待,它才停止
text(value) {
value
? this.socket.emit("sender_typing", {
receiver: this.userPop.first_name,
receiver_id: this.userPop._id,
sender_id:this._id,
sender: this.user.first_name,
senderSocketId: this.socket.id,
})
: this.socket.emit("stopTyping");
},
mounted() {
this.socket.on("typing", async (data) => {
await console.log(data.receiver_id , data.sender_id);
if (this._id === data.receiver_id) {
this.typing = data.sender;
}
});
this.socket.on("stop_typing", () => {
this.typing = "";
});
// this.socket.on("seen_server", (data)=>{
// this.seen= data.message
// })
},
socket.on("new-message", async(data)=>{
await console.log(data)
await Message.create(data)
.then(async (msg) => {
return msg.save();
})
.then(async (msg) => {
const newMsg = await msg.toObject();
console.log(newMsg);
socket.emit('fetch-message',{newMsg})
})
.catch(err=> console.log(err))
})
socket.on("sender_typing", async(data) => {
await console.log(data)
socket.emit('typing', {
sender: data.sender,
sender_id:data.sender_id,
socket_id: socket.id,
receiver_id:data.receiver_id,
})
});