【问题标题】:Node.js and Socket.io - cannot read property 'sockets of undefinedNode.js 和 Socket.io - 无法读取属性“未定义的套接字”
【发布时间】:2018-04-07 17:16:20
【问题描述】:

我对 node.js 还是很陌生,希望你能帮助我。

我使用 Node (v7.9.0) 与模块 express (4.15.2) 和 socket.io (1.7.3) 来创建一个简单的网络服务器并将事件从网络服务器发送到连接的客户端。

我正在尝试模块化我的应用程序,并希望在不同的 js 文件上向客户端发出不同的事件。

现在这是我当前的代码:

--- start.js:

// Configure the build-in webserver
var 
  express = require('express'),
  app = express(),
  server = require('http').createServer(app),
  io = require('socket.io').listen(server),
  conf = require('./serverConfig.json');

module.exports = server;            // Export to use with uart_functions.js

module.exports.io = io;             // Export to use with uart_functions.js

server.listen(conf.port);           // Configure the server to port 8080

app.use(express.static(__dirname + '/pages'));  // All the website data is in this folder

app.get('/', function(req, res) {
    res.sendfile(__dirname + '/pages/index.html');      // Webserver should return the page index.html
});

console.log("Server is opened: http://127.0.0.1:" + conf.port +"/\n");

io.sockets.on('connection', function(socket) {
    console.log("Verbunden!!!\n");
    io.sockets.emit('chat', {zeit: new Date(), name: "Server", text: "Du bist nun mit dem Server verbunden!"}); // Send Data to Client. This works
});

--- uart_functions.js:

var ioSocket = require('./start.js').io;

function TelegrammID1() {
    console.log("RUN ID 1\n");
    ioSocket.sockets.emit('chat', {zeit: new Date(), name:'Tester', text: "Das ist ein Test"}); // Send data to client - This doesn't work    
}

现在在 uart_functions.js 文件中出现错误:“TypeError: Cannot read property 'sockets' of undefined.”

我了解错误发生的原因。但我不知道我仍然需要通过 module.export 将文件 start.js 传递给文件 uart_functions.js 的内容和方式

关于搜索我发现如下:Cannot read property 'socket's of undefined when exporting socket.io

很遗憾,没有有用的答案。 感谢您的帮助

【问题讨论】:

    标签: node.js


    【解决方案1】:

    问题是您在函数TelegrammID1 之外需要ioSocket,该函数在建立连接时再次导出以运行。在函数TelegrammId1 中定义变量ioSocket 并确保导出该函数。

     function TelegrammID1() {
       var ioSocket = require('./start.js').io;
       console.log("RUN ID 1\n");
       ioSocket.sockets.emit('chat', {zeit: new Date(), name:'Tester', text: "Das ist ein Test"}); // Send data to client - This doesn't work
    }
    

    【讨论】:

    • 太棒了! @Rohith,我永远不会想到这会引起问题。为我工作!谢谢。
    【解决方案2】:

    不太确定你的方法是否正确。导出如下

        // Configure the build-in webserver
        var 
          express = require('express'),
          app = express(),
          server = require('http').createServer(app),
          io = require('socket.io').listen(server),
          conf = require('./serverConfig.json');
    
       // module.exports = server;            // Export to use with uart_functions.js
    
       // module.exports.io = io;             // Export to use with uart_functions.js
    
        server.listen(conf.port);           // Configure the server to port 8080
    
        app.use(express.static(__dirname + '/pages'));  // All the website data is in this folder
    
        app.get('/', function(req, res) {
            res.sendfile(__dirname + '/pages/index.html');      // Webserver should return the page index.html
        });
    
        console.log("Server is opened: http://127.0.0.1:" + conf.port +"/\n");
    
        io.sockets.on('connection', function(socket) {
            console.log("Verbunden!!!\n");
            io.sockets.emit('chat', {zeit: new Date(), name: "Server", text: "Du bist nun mit dem Server verbunden!"}); // Send Data to Client. This works
        });
    
        module.exports = {
         io: io,
         server:server
        }
    

    【讨论】:

      【解决方案3】:

      感谢您的帮助。

      使用此代码:

        module.exports = 
        {
          io: io,
          server:server
        }
      

      uart_functions.js 中的这一行:var ioSocket = require('./start.js'); 我收到以下错误:“TypeError: Cannot read property 'emit' of undefined”

      我还尝试了以下导出:

      module.exports = 
        {
          io: io,
          server:server,
          app: app,
          express: express
        }
      

      但这会返回相同的错误....

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-22
        • 2019-10-20
        相关资源
        最近更新 更多