javascript - 如何使用 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);
});
解决方案
推荐阅读
- django - 创建具有深度选项支持的相同类型的嵌套序列化程序
- go - 无法使用 CGO AMD=> ARM 与 Xgo 交叉编译 Go 项目
- blockchain - 如何获取存储在区块链中的数据,然后显示在网页上?
- python - 如何计算字典中的常见值?
- python - 如何将 pyfakefs 与 importlib.resources.path 结合使用
- google-apps-script - 如何设置单元格格式(作为日期和时间差格式)
- javascript - 使用纯 JavaScript 将音频转换为 wav/mp3
- java - 使用 Hibernate 但具有不同列的多个连接
- reactjs - 使用 react-router-dom 在组件基础上键入 location.state 属性
- mysql - MySQL - 单元格中的数组