2017-04-25 77 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 
    } 

但這返回相同的錯誤....