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

前端 未结 1 1402
梦如初夏
梦如初夏 2021-01-27 05:39

Create e.g. assets/js/dependencies/app.io.js with:

io.socket.on(\'connect\', function socketConnected() {
  console.debug(\"This is from the connect         


        
相关标签:
1条回答
  • 2021-01-27 06:24

    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

    0 讨论(0)
提交回复
热议问题