【问题标题】:Connect from a client to a sails based server从客户端连接到基于 Sails 的服务器
【发布时间】:2015-03-24 03:49:43
【问题描述】:

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

<!-- 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>

当我打开浏览器时出现此错误:

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


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

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

【问题讨论】:

  • 您使用的是什么版本的 Sails 和sails.io.js?
  • 我使用的是 Sails.js 的 0.10.5 版本,我假设是最新版本的sails.io,因为我几天前才安装它

标签: cordova socket.io sails.js sails.io.js


【解决方案1】:

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

这里最简单的做法是下载 Sails 将使用的文件,您可以得到 here。

【讨论】:

  • 非常感谢!我不敢相信解决方案这么简单。
  • 我的荣幸。我会检查这是否也解决了您的otherissues...
【解决方案2】:

同样的问题,我有一个运行良好的sailsjs 服务器,只是想将一个node.js 应用程序连接到它。我尝试了以下代码,发现 here 链接每个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)
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2013-12-26
    • 2023-04-09
    相关资源
    最近更新 更多