【发布时间】:2016-04-08 09:25:22
【问题描述】:
我正在使用 Node.js、Express 和 Socket.io 制作一些单页 Web 应用程序。 我想在浏览器中显示工作情况。 在 IDE 中,有控制台,所以我可以在控制台窗口中检查程序进程。就像,我想向浏览器展示这些过程。我想要的只是“发射”。
当我在app.js 文件中使用socket.io 时,没有问题。但这对我来说是有限的。我想在运行时实时显示许多句子。如何在 app.js 和 controller.js 中使用 socket.io?我红了Use socket.io in controllers这篇文章,但是看不懂。请帮我一个简单的解决方案。
app.js
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
module.exports.io = io;
...
controller.js
var io = require('./app').io;
// some task
console.log('Task is done!'); // it would be seen in console window
io.sockets.emit('Task is done!'); // Also I want to display it to broswer
结果(错误)
TypeError: Cannot read property 'sockets' of undefined
编辑 2---
跟随 Ashley B 的 cmets,我是这样编码的。
controller.js
module.exports.respond = function(socket_io) {
socket_io.emit('news', 'This is message from controller');
};
app.js
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var controller = require('./controller');
io.sockets.on('connection', controller.respond );
它运作良好,但我想知道的是......当我想要几个socket_emit时,我应该怎么做?我应该每次都打电话吗?如果你不明白。见下文:
//first task is done
module.exports.respond = function(socket_io) {
socket_io.emit('news', 'First task is done!');
};
//second task is done
module.exports.respond = function(socket_io) {
socket_io.emit('news', 'Second task is done!');
};
//third task is done
module.exports.respond = function(socket_io) {
socket_io.emit('news', 'Third task is done!');
};
但这是错误的方式,对吧?只有最后一个 api 在app.js 中实现。我的控制器中有很多console.log,我想将其转换为socket.emit,我该怎么做?
【问题讨论】:
-
类似的东西应该可以工作 - app.js:
require('./controller.js)(io)然后在 controller.js 上你会这样做:module.exports = function(io) { // socket stuff here// } -
@Ashley B 我不明白....我需要调用 module.exports 每个日志吗?我无法猜测“套接字的东西”我必须输入什么。
-
您可以在其中添加
io.sockets.emit('Task is done!');,就像您在示例中使用的那样。 -
@AshleyB 你能检查一下添加吗?我需要调用 module.exports 每个日志吗?
-
你会这样做:
module.exports.respond = function(socket_io) { socket_io.emit('news', 'First task is done!'); socket_io.emit('news', 'Second task is done!'); };我建议你阅读如何在 node.js 中进行导出
标签: node.js express socket.io mean-stack