Sails.io.js io.socket.get('/user',…) Not implemented in core yet

别说谁变了你拦得住时间么 提交于 2019-12-02 10:54:59

we need more informations such as:

  1. are you using js frameworks like Angular?
  2. are you managing your dependencies with tools like Bower?
  3. how is your Sails server configured?

Above all i can show you how i configured my Sails backend:

in my .sailsrc i have the hooks configured as follows

"hooks": {
"csrf": false,
"grunt": false,
"i18n": false,
"pubsub": false,
"session": false,
"sockets": true,
"views": false}

then in my UserController.js i have this simple method that enables the socket communication

enableNtofications: function(req, res){

    // checking if the request comes from
    // a socket request
    if(req.isSocket){

        // getting the current logged user
        var user = req.user;

        // subuscribing the client to model changes
        User.subscribe(req, [user.id]);

        return res.ok();

    } else {
        return res.badRequest();
    }

},

my frontend uses Angular and a the ngSails module that is a sort of wrapper of 'sails.io' for Angular

and in my 'UserService.js' i can do something like

        // waiting for notifications on User
        $sails.on('user', function(event) {
            if (event) {
                // manage the message here...
            }
        });

and then call the server method in order to enable the sockets

        // calling the service
        return $sails.post('/user/enableNtofications').then(
            // ok
            function() {},
            // ko
            function(data) {
                console.log("enable notifications KO");
                console.log(data.error);
            });

(you need also to inject the '$sails' module and configure it properly...)

Hope this could help you

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!