Using io.socket.get in sails js socket.io

I am using sailsjs with socket io. The sail version is 0.10.5. I have the following socket client for testing:

var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.js');

// Instantiate the socket client (`io`)
// (for now, you must explicitly pass in the socket.io client when using this library from Node.js)
var io = sailsIOClient(socketIOClient);

// Set some options:
// (you have to specify the host and port of the Sails backend when using this library from Node.js)
io.sails.url = 'http://localhost:1337';
// ...

//Send a GET request to `http://localhost:1337/hello`:
io.socket.get('/join', function serverResponded (body, JWR) {
  // body === JWR.body
  console.log('Sails responded with: ', body);
  console.log('with headers: ', JWR.headers);
  console.log('and with status code: ', JWR.statusCode);

  // When you are finished with `io.socket`, or any other sockets you connect manually,
  // you should make sure and disconnect them, e.g.:
  //io.socket.disconnect();

  // (note that there is no callback argument to the `.disconnect` method)
});


io.socket.on('myroom', function(response){
  console.log(response);
});

In my config / routes.js, I have the following:

'get /join':'LayoutController.join'

In my api / controller / LayoutController, I have the following:

module.exports={

    join: function(req, res) {
        console.log('Inside Join');

        sails.sockets.join(req.socket,'myroom');
         res.json({message:'youve subscribed to a room'}); 
      }
    };

The problem that I encountered is that the connection function inside the controller never starts via the socket call io.socket.get ('/ join', ...). The .log console in the controller never prints, nor does it return a response to the client. If I do the same testing via http, it starts the connection function. This is the first time I'm working with a jack. Can anyone suggest what I am doing wrong here?

+4
source share
1

, , . :

var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.js');

var io = sailsIOClient(socketIOClient);

// Set some options:
// (you have to specify the host and port of the Sails backend when using this library from Node.js)
io.sails.url = 'http://localhost:1337';
// ...

//Send a GET request to `http://localhost:1337/hello`:
io.socket.on('connect', function(){
  io.socket.get('/join', function serverResponded (body, JWR) {
    // body === JWR.body
    console.log('Sails responded with: ', body);
    console.log('with headers: ', JWR.headers);
    console.log('and with status code: ', JWR.statusCode);

    // When you are finished with `io.socket`, or any other sockets you connect manually,
    // you should make sure and disconnect them, e.g.:
    //io.socket.disconnect();

    // (note that there is no callback argument to the `.disconnect` method)
  });
});


io.socket.on('myroom', function(response){
  console.log(response);
});

. , !

0

Source: https://habr.com/ru/post/1611240/


All Articles