首页 > 解决方案 > 如何使用 socket.io 向特定客户端发送通知?

问题描述

我正在尝试在用户收到好友请求时向用户发送通知。我面临的问题是服务器正在发送数据,但无法在前端显示。

这是服务器端代码

module.exports=function(io) {

  io.on('connection',(socket)=>{
    socket.on('joinRequest',(myRequest,callback)=>{
       socket.join(myRequest.sender);
       callback();
    });

    socket.on('friendRequest',(friend,callback)=>{
      io.to(friend.receiver).emit('newFriendRequest',{
        from:friend.sender,
        to:friend.receiver
      });
      console.log('new friend request sent');
      callback();
    });
  });
}

这是客户端的代码

socket.on('newFriendRequest',function(friend){
  console.log('heyy got you');
  console.log(friend);
});

标签: javascriptnode.jssocket.io

解决方案


推荐阅读