2017-04-25 28 views
0

我对node.js仍然很陌​​生,希望你能帮助我。Node.js和Socket.io - 无法读取undefined的属性套接字

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

我试图模块化我的应用程序,并想在不同的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:无法读取未定义的属性'套接字'。”

我明白为什么会发生错误。但我不知道是什么,以及如何我还是通过module.export越过文件start.js到文件uart_functions.js

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

不幸的是没有有用的答案。 感谢您的帮助

回答

0

不太确定您是否正确的做法。导出如下

// 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 
    } 
0

感谢您的帮助。

与此代码:

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

而此行的uart_functions.js:var ioSocket = require('./start.js'); 我geht以下错误: “类型错误:无法读取属性 '发射' 的未定义”

我也尝试以下导出:

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

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

相关问题