Cordova 从客户端连接到基于sails的服务器

Cordova 从客户端连接到基于sails的服务器,cordova,socket.io,sails.js,sails.io.js,Cordova,Socket.io,Sails.js,Sails.io.js,我正在尝试使用库sails.io从cordova客户端连接到基于sails的服务器。 这是我的客户代码: 它一直试图重新连接,但没有停止。 我已经使用socket.io在我的服务器上建立了一个聊天室,但那是本地的,所以我很确定它与cors有关。这里也有同样的问题,我有一个运行良好的sailsjs服务器,只想将node.js应用程序连接到它。我尝试了下面的代码,这些代码是根据sails.js网站链接的,我得到了相同的回复 我认为它可能来自socket.io-client版本,因为npm列表建议s

我正在尝试使用库sails.io从cordova客户端连接到基于sails的服务器。 这是我的客户代码:

它一直试图重新连接,但没有停止。
我已经使用socket.io在我的服务器上建立了一个聊天室,但那是本地的,所以我很确定它与cors有关。

这里也有同样的问题,我有一个运行良好的sailsjs服务器,只想将node.js应用程序连接到它。我尝试了下面的代码,这些代码是根据sails.js网站链接的,我得到了相同的回复

我认为它可能来自socket.io-client版本,因为npm列表建议sails使用0.9.17版本的socket.io,而我使用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)
});

同样的问题,我有一个运行良好的sailsjs服务器,只想将node.js应用程序连接到它。我尝试了下面的代码,这些代码是根据sails.js网站链接的,我得到了相同的回复

我认为它可能来自socket.io-client版本,因为npm列表建议sails使用0.9.17版本的socket.io,而我使用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)
});

您的问题可能是您的Sails版本与Sails.io.js版本不匹配。当您生成带有Sails generate new的Sails v0.10.5应用程序时,它将为您提供相应版本的Sails.io.js。但是,如果您只是npm安装sails.io.js,您将获得最新版本,该版本旨在使用Socket.io 1.0与即将发布的sails v0.11版本配合使用


这里最简单的方法是下载Sails将使用的文件,您可以获得该文件。

您的问题可能是您的Sails版本和Sails.io.js版本不匹配。当您生成带有Sails generate new的Sails v0.10.5应用程序时,它将为您提供相应版本的Sails.io.js。但是,如果您只是npm安装sails.io.js,您将获得最新版本,该版本旨在使用Socket.io 1.0与即将发布的sails v0.11版本配合使用


这里最简单的方法就是下载Sails使用的文件,您可以获得。

您使用的是什么版本的Sails和Sails.io.js?我使用的是0.10.5版的Sails.js,我假设是最新版本的Sails.io,因为我几天前才安装了它。您使用的是什么版本的Sails和Sails.io.js?我使用的是0.10.5版的Sails.js,我假设是最新版本的Sails.io,因为我只有几天前就安装好了,非常感谢!我真不敢相信解决办法这么简单。我很高兴。我会检查这是否也解决了你的问题…非常感谢!我真不敢相信解决办法这么简单。我很高兴。我会检查这是否也解决了你的问题。。。
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)
});