【问题标题】:How to watch for new records added to model in Sails.js如何在 Sails.js 中查看添加到模型的新记录
【发布时间】:2015-05-11 10:12:46
【问题描述】:

我正在尝试在用户向用户聊天消息系统中的某个特定 ID(在用户模型内)发送消息时推送消息。我无法让它工作,也不知道如何开始。我的Messaging 控制器中有这个:

sendAndReceiveMsgs: function(req, res) {

    if (req.isSocket && req.param('message') && req.param('to')) {

        var message = req.param('message'),
            from = req.user.id,
            to = req.param('to');

        Messaging.create({ message: message, from: from, to: to })
            .exec(function (err, created) {
                console.log('sent');
        });

    } else if (req.isSocket) {

        Messaging.find({ where: { to: req.user.id }, limit: 1, sort: 'id DESC' })
        .exec(function(err, messages) {
            if(messages.length > 0){
                console.log(message.length + " new messages");
            } else {
                console.log("No new messages");
            }
            Messaging.subscribe(req.socket, message);
            Messaging.watch(req);
            Messaging.publishCreate({ id: message[0].id, message: message[0].message });
        });

    } else if (!req.isSocket) {

        return res.notFound();

    }

}

但是,它不会向用户(针对他)推送更多新消息。有什么线索吗?我真的不明白这一点,也不知道从哪里开始。谢谢!

【问题讨论】:

    标签: javascript node.js websocket sails.js messages


    【解决方案1】:

    请也显示客户端代码。 我注意到的一件事是您正在使用订阅,但它仅用于查看由 .publishUpdate()、.publishDestroy()、.publishAdd() 和 .publishRemove() 发出的消息。不确定这是否有帮助

    【讨论】:

      【解决方案2】:

      您需要在客户端中使用 io.socket.on。请参阅文档here。 如果您使用 pub/sub 方法,则 eventIdentity 也是模型名称。

      【讨论】:

        猜你喜欢
        • 2014-11-14
        • 2015-07-04
        • 2021-05-14
        • 1970-01-01
        • 2019-07-02
        • 1970-01-01
        • 2013-09-18
        • 1970-01-01
        • 2021-11-26
        相关资源
        最近更新 更多