我有以下代码:
//app.js
var ioroutes = require('./controllers/socket');
sIo.sockets.on('connection', ioroutes.connection);
//socket.js
exports.connection = function(socket){
console.log('I have ' + socket);
};
现在,从app.js我可以访问sIo对象,我想用它来查看我连接的客户端数量。如何将sIo对象传递给导出的“连接”函数?
提前致谢。
答案 0 :(得分:0)
您需要将其作为参数添加到连接函数中:
//app.js
var ioroutes = require('./controllers/socket');
sIo.sockets.on('connection', function(socket) {
ioroutes.connection(socket, sIo);
});
//socket.js
exports.connection = function(socket, sIo){
console.log('I have ' + socket);
};