【发布时间】:2015-11-18 02:46:48
【问题描述】:
每当创建(或删除/修改)模型时,每个连接的套接字都会通过 Sails 自动监视设置得到通知。这在某种程度上很好,但我想在某个时候过滤这些通知。
我的应用程序有自己的“通知”,应该发送给各自的接收者。所以他们的解剖结构有点像:id, message, receiver, sender。
身份验证是本地护照实现。
侦听notification 事件会导致每次创建通知时都会收到通知。
// client: app.js
io.socket.on('notification', function(evt) { console.log(evt); });
我现在尝试实现的是过滤这些通知以匹配用户 ID。我编写了一个适用于/notification 事件的策略。
// Policy: forUser
module.exports = function(req, res, next) {
// ... whatever ... //
return next();
}
在政策范围内
'notification': {
'create': ['passport', 'forUser']
}
我现在的问题是:如何实施这个政策?我想只检查notification.receiver == req.user.id,但是如何在策略中获取通知模型(如果这是正确的方法)?
谢谢。
编辑:尝试实施房间解决方案,但我没有在客户端收到任何通知。
我在 NotificationController 中更改了订阅功能:
subscribe: function(req, res) {
sails.log.info('Your user id: ' + req.user.id);
sails.sockets.join(req.socket, 'user_notifications_' + req.user.id);
res.json({
room: 'user_notifications_' + req.user.id
});
},
并为我的模型添加了afterCreate 方法:
afterCreate: function(model, next) {
sails.sockets.broadcast('user_notifications_' + model.receiver, { test: 'hello' });
next();
}
客户端上的代码现在是:
io.socket.get("/notification/subscribe", function(data, jwr) {
io.socket.on(data.room, function(obj) {
console.log(obj);
});
});
调用订阅方法并返回正确的房间名称。但是我在拨打/notification/create?message=test&receiver=1 时没有收到任何消息。调用了afterCreate 方法,所有用户id 都是正确的(因为只有一个用户),但是什么也没有发生。
编辑2: 好像加入房间失败了。
sails.sockets.join(req.socket, 'testroom');
// For testing
sails.log.debug(sails.sockets.socketRooms(req.socket));
房间已创建,但套接字未订阅。
编辑3: 找到了解决方案。界面完成后我会第一时间发布 GitHub 链接。
【问题讨论】:
-
您希望此策略过滤传出的 websocket 消息吗?这些策略不适用于事件,而是适用于控制器。当 socket.io 向客户端发送消息时,他们无法控制会发生什么。我在你的问题中遗漏了什么吗?
-
不,没错。我想过滤传出的套接字消息。如果策略无法做到这一点,那么过滤套接字消息的正确方法是什么?
标签: javascript node.js sockets sails.js