如何在使用model.subscribe时指定条件

时间:2015-07-06 16:35:21

标签: node.js socket.io sails.js

我正在尝试使用model.subscribe将套接字客户端订阅到特定事件: http://sailsjs.org/documentation/reference/web-sockets/resourceful-pub-sub/subscribe

但有没有办法订阅套接字客户端只能创建具有特定条件的项目?

例如我有一个Message模型: 属性: -类型 -所有者 -信息 -ID -createdAt -updatedAt

有没有办法只订阅使用所有者1或2创建的消息?

我试过了:

Message.find({owner: 1}).exec(function(e, messages){
    Message.subscribe(req.socket, messages, ['create']);
});

在日志中,我收到了类似的内容:

2015.07.06 16:05:29 SILLY Subscribed to the Message with id=3 (room :: sails_model_message_3:create)
2015.07.06 16:05:29 SILLY Subscribed to the Message with id=4 (room :: sails_model_message_4:create)
2015.07.06 16:05:29 SILLY Subscribed to the Message with id=5 (room :: sails_model_message_5:create)
2015.07.06 16:05:29 SILLY Subscribed to the Message with id=6 (room :: sails_model_message_6:create)
2015.07.06 16:05:29 SILLY Subscribed to the Message with id=7 (room :: sails_model_message_7:create)

但是当创建一个owner = 1的新消息时,我的订阅者套接字不会收到该事件。

客户代码:

io.socket.get('/messages/get');
io.socket.on('message',function(obj){
    if(obj.verb === 'created'){
        $log.info(obj)
    }
});

创建代码:

Message.create(data).exec(function(error, data){
    Message.publishCreate({id: data.id, owner: data.owner, message : data.message , type: data.type});
});

或者也许手表更好?我可以使用手表但在那里我无法指定任何标准。 http://sailsjs.org/documentation/reference/web-sockets/resourceful-pub-sub/watch

0 个答案:

没有答案