问题描述
我有一个名为 Groupfeed 的模型,它看起来像这样
I have a model named Groupfeed which looks like this
module.exports = {
schema:true,
attributes:
{
groupid:
{
model:'groups',
required:true
},
postid:
{
model:'post',
required:true
},
objectid:
{
model:'objects',
required:true
},
}
};
在客户端,我可以使用
On the client side I can subscribe to the Groupfeed model using
io.socket.get('/groupfeed')
这是由蓝图api自动完成的然后
which is done automatically by the blueprint apiand then
io.socket.on('groupfeed',function(obj){console.log(obj)})
当我使用
Groupfeed.publishCreate({id:4,groupid:6,postid:2,objectid:1})
我想要什么:-
What I want :-
我希望客户只订阅来自特定 groupid 的 groupfeed.例如:用户 X 可以从 groupid 1 订阅 groupfeeds(注意:组模型存储组的用户成员资格)
I want a client to subscribe only to groupfeeds from a particular groupid.Eg: User X can subscribe to groupfeeds from groupid 1 (Note: A group model stores user membership for a group )
或者类似这个想象中的调用:
OR something like this imaginary call:
io.socket.get('/groupfeed?groupid=5')
这样当我使用 groupid:5 调用 publishCreate 时,只有订阅 groupid 5 的 groupfeed 的人才能获得更新
So that when I call publishCreate with a groupid:5, only people subscribed to groupid 5's groupfeed could get an update
推荐答案
你最好为小组创建不同的房间.
You better create different rooms for groups.
代码未经测试!创建控制器:通知控制器.js
CODE UNTESTED!Create a controller:NotificationsController.js
module.exports = {
subscribe: function(req, res) {
// Get groupId of user by your method
.....
.....
var roomName = 'group_' + groupId;
sails.sockets.join(req.socket, roomName);
res.json({
room: roomName
});
}
}
您可以在某处创建通知:
Somewhere you can create notification:
var roomNameForGroup = 'group_' + groupId;
sails.sockets.blast(roomNameForGroup, {id:4,groupid:6,postid:2,objectid:1});
在您看来:
io.socket.on('connect', function(){
io.socket.get('/notifications/subscribe', function(data, jwr){
if (jwr.statusCode == 200){
io.socket.on(data.room,function(obj){
console.log(obj);
});
} else {
console.log(jwr);
}
});
});
我现在无法测试代码,但它看起来可行.
I can not test the code right now, but it looks workable.
这篇关于Sails js 订阅由 groupid 属性限定的模型更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!