通过测试将插座连接到风帆

时间:2016-05-10 11:20:26

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

我正在尝试从mocha测试sails套接字连接。

    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';

它出错

  

未捕获错误:提供的socket.io客户端(io)已经存在   扩充到Sails套接字SDK实例(它有io.sails)。

我做错了吗? sails版本0.12.3,socket.io-client 1.4.6

2 个答案:

答案 0 :(得分:0)

您的问题是可行的,因为您已安装多个sails.io.js

您应该在sails.io.js

中查看此行代码

查看您的代码

答案 1 :(得分:0)

我曾经遇到过这个问题。它发生在我使用套接字连接到 React 时。

一个快速的解决方法是只调用一次 io 函数,即在 React 中,它通过将 sockets 函数包装在 componentDidMount 函数中来确保它即使刷新也只运行一次。

确保只运行一次

 var io = sailsIOClient(socketIOClient);
 io.sails.url = 'http://localhost:1337';

这样做

    let io;
if (socketIOClient.sails) {
  io = socketIOClient;
} else {
  io = sailsIOClient(socketIOClient);
}

你可以告别那个讨厌的错误

相关问题