将socket.io共享给其他模块会产生空对象

时间:2013-12-09 01:17:20

标签: javascript node.js sockets module socket.io

我正在尝试在不同的node.js模块中共享socket.io的套接字对象,尽管我失败并且使用

获取空对象
Cannot call method 'on' of undefined

我的代码:

app.js

var express = require('express')
  , app = express();

var server = require('http').createServer(app)
  , io = require('socket.io').listen(server)

var routes = require('./routes')
  , path = require('path')
  , rss = require('./routes/rss')

// ...

exports.io = io;

路由/ rss.js

io = require(__dirname + '/../app');

console.log(io);
io.sockets.on('connection', function(
  console.log("Connection on socket.io on socket");
  // .. do stuff 
});

这是我从中获得的输出:

$ node app.js                                                                                                    
   info  - socket.io started
{}

/home/XXX/programming/nodejs/node-express-aws/routes/rss.js:10
io.sockets.on('connection', function(socket){
           ^
TypeError: Cannot call method 'on' of undefined
    at Object.<anonymous> (/home/XXX/programming/nodejs/node-express-aws/routes/rss.js:10:12)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/XXX/programming/nodejs/node-express-aws/app.js:9:10)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)

虽然我已尝试过,但我只能在一个(app.js)文件中使用socket.io

var express = require('express')
  , app = express();

var server = require('http').createServer(app)
  , io = require('socket.io').listen(server)

var routes = require('./routes')
  , path = require('path')
  , rss = require('./routes/rss')

// ...

io.sockets.on('connection', function(socket){
  logger.debug("Connection on socket.io on socket");
  socket.emit('news', {will: 'be recived'});
});

2 个答案:

答案 0 :(得分:16)

因为,在app.js中,你有:

exports.io = io;

然后你需要像这样使用它:

var app = require('../app');
var io = app.io;

就是说,你将一个名为io的属性附加到模块中,所以当你需要那个模块时,你会得到一个具有io属性集的对象

您也可以

module.exports = io;

然后立即离开rss.js


所有这一切,如果你使用Node运行app.js,你会更常见的是注入其他模块的io对象(而不是相反);例如:

<强> app.js

var express = require('express')
  , app = express();

var server = require('http').createServer(app)
  , io = require('socket.io').listen(server)

var routes = require('./routes')
  , path = require('path')
  , rss = require('./routes/rss')

// ...

rss(io); // pass `io` into the `routes` module,
         // which we define later to be a function
         // that accepts a Socket.IO object

<强> routes/rss.js

module.exports = function(io) {
  io.sockets.on('connection', function(
    console.log("Connection on socket.io on socket");
    // .. do stuff 
  });
}

答案 1 :(得分:3)

我将io对象传递给connection处理程序。

var socket = require('./routes/socket.js');

io.sockets.on('connection', function(sock){
    socket.stuff(sock, io);
});

./ routes / socket.js应包含以下内容:

var socket = module.exports = {};

socket.stuff = function(sock, io){
    //handle all events here
};