Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 将数据发送到socket.io中的特定用户id_Node.js_Reactjs_Socket.io - Fatal编程技术网

Node.js 将数据发送到socket.io中的特定用户id

Node.js 将数据发送到socket.io中的特定用户id,node.js,reactjs,socket.io,Node.js,Reactjs,Socket.io,Hii我正在尝试编写一个函数,通过特定用户的id向其发送好友请求,但它不起作用。它不显示任何错误,但对特定用户不起作用 服务器文件 socket.on("friendRequest", (friend) => { io.sockets.to(friend.receiver).emit("newFriendRequest", { from: friend.sender, to: friend.receiver }); con

Hii我正在尝试编写一个函数,通过特定用户的id向其发送好友请求,但它不起作用。它不显示任何错误,但对特定用户不起作用

服务器文件

socket.on("friendRequest", (friend) => {
      io.sockets.to(friend.receiver).emit("newFriendRequest", {
        from: friend.sender,
        to: friend.receiver
      });
      console.log("Reciever", friend.receiver)
    });
发出事件的客户端文件

  socket.emit("friendRequest", {
            receiver: id,
            sender: this.state.profile._id
        })
另一个在客户端保存新好友请求的文件

  socket.on("newFriendRequest", (newRequest) => {
        setNewRequest([...newRquest, newRequest])
        console.log("Friend request sended", newRquest)
      })

id
确实是存在的套接字的id吗?尝试注销当前套接字
io.sockets.clients()
No它是在中生成的特定用户idmongoDB@JanardanPrajapti您是否将MongoDB用作套接字存储?不,事实上,我对socket.io的功能了解不多。我将数据从一个用户发送到一个特定的用户id,该id在mongoDB中默认生成。我没有使用套接字id,因为我不知道如何使用它。@JanardanPrajapti在这种情况下,
sockets.to(friend.receiver)
不起作用,socket.io在这里需要客户端套接字id。为了路由消息,您需要跟踪客户机/服务器上的套接字ID