0

为了测试应用程序,我为一个用户使用 Chrome 普通窗口,为另一个用户使用隐身窗口。非套接字使用隐身窗口向用户发送数据。然而,它们在前端和后端之间发送数据,但仅限于调用它们的用户。我使用同一台本地机器进行测试的方式是否错误。

这是一些片段:

客户端:

  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 = "";
 this.socket.on("fetch-message", async (data) => console.log(data));
 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,
    })
    
  });

4

0 回答 0