Sails.io.js io.socket.get('/user',…) Not i

2020-05-03 10:46发布

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

io.socket.on('connect', function socketConnected() {
  console.debug("This is from the connect: ", io.socket);
  console.debug("WebSocket is connected:", io.socket.isConnected());

  io.socket.get('/user', function(resData) {
    console.debug(resData);
  });
});

Console

  |>    Now connected to Sails.
\___/   For help, see: ....
        (using sails.io.js browser SDK @v0.13.7)

app.io.js:3 This is from the connect:  SailsSocket {headers: undefined, eventQueue: Object, isConnecting: false, extraHeaders: Object, hostname: "localhost"…}
app.io.js:4 WebSocket is connected: true
app.io.js:7 Not implemented in core yet    <========= WHY?

NOTE: io-socket-get documentation

Why am I getting this message?

Any pointers on how to fix this?

1条回答
ゆ 、 Hurt°
2楼-- · 2020-05-03 11:13

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

查看更多
登录 后发表回答