Connect from a client to a sails based server

2019-06-04 21:22发布

I am trying to connect to my sails based server from my cordova client using the library sails.io. this is my client code:

<!-- cordova script (this will be a 404 during development) -->
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>
<script src="lib/angular-socket-io/socket.js"></script>
<script src="lib/angular-sails/src/ngSails.js"></script>
<script src="lib\socket.io-client\socket.io.js"></script>
<script src="lib/sails.io.js/sails.io.js"></script>
<script type="text/javascript">
    io.sails.useCORSRouteToGetCookie = false;
    io.sails.url = 'http://178.62.83.248:1337/';
</script>

when i open the browser i get this error:

        Socket is trying to reconnect to Sails...
_-|>_-  (attempt #1)


  sails.io.js:136  
    Socket is trying to reconnect to Sails...
_-|>_-  (attempt #2)

and it keep trying to reconnect(it doesn't stop). i have already build a chat on my server using socket.io but that was locally so i'm pretty sure its has something to do with cors.

2条回答
ら.Afraid
2楼-- · 2019-06-04 21:39

Your problem is likely a mismatch between your version of Sails and your version of sails.io.js. When you generate a Sails v0.10.5 app with sails generate new, it gives you the appropriate version of sails.io.js. But if you just npm install sails.io.js, you'll get the bleeding-edge version which is designed to work with the imminent v0.11 release of Sails, using Socket.io 1.0.

Easiest thing to do here would be just download the file that Sails would use, which you can get here.

查看更多
老娘就宠你
3楼-- · 2019-06-04 21:50

Same issue here, i ve got a sailsjs server thats running fine, just wanted to connect a node.js app to it. I tried the following code found here as linked per sails.js website and I get the same responses.

I think it might be from socket.io-client version as a npm list suggest that sails uses 0.9.17 version of socket.io and I use 1.3.2.

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('/', 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)
});
查看更多
登录 后发表回答