在 Express.js 项目中使用 Socket.io [英] Using Socket.io in Express.js Project

查看:31
本文介绍了在 Express.js 项目中使用 Socket.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Node.js、Express 和 Socket.io 制作一些单页 Web 应用程序.我想在浏览器中显示工作情况.在IDE中,有控制台,所以我可以在控制台窗口中检查程序进程.就像,我想向浏览器展示这些过程.我想要的只是发射".

I'm making some single page web application with Node.js, Express and Socket.io. I want to display how works are going to browser. In IDE, There is console, so I can check program process in console window. Like, I want to show these process to browser. All I want to is just 'emit'.

当我在 app.js 文件中使用 socket.io 时,没有问题.但这对我来说是有限的.我想在运行时实时显示许多句子.如何不仅在 app.js 中而且在 controller.js 中使用 socket.io?我红色在控制器中使用socket.io这篇文章,但我看不懂.请帮我一个简单的解决方案.

When I use socket.io in app.js file, there is no problem. but this is limited for me. I want to display many sentences in realtime as running. How can I use socket.io in not only app.js but controller.js? I red Use socket.io in controllers this article, but I can't understand. Please help me with a simple solution.

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 的评论,我是这样编码的.

Follwoing Ashley B's comments, I coded like this.

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时,我该怎么办?我应该每次都打电话吗?如果你不明白.见下文:

It works well, But what I wonder is... When I want to several socket_emit, What Should I do? Should I call everytime? If you don't understand. see below :

//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!');
 };

但这是错误的方式,对吧?app.js 中只实现了最后一个 api.我的控制器中有很多console.log,我想把它转换成socket.emit 我该怎么做?

But It is wrong way, right? only last api is implemented in app.js. There are a lot of console.log in my controller, I want to convert it to socket.emit How can I do this?

推荐答案

我已经使用 socket.ionodejs 事件 解决了这个问题.这个想法是只处理来自 app.js 的 socket 的发射,并从控制器发射 nodejs 事件,这些事件将在 app.js 中捕获,然后由插座.

I have worked on this using socket.io and nodejs events. The idea is to handle the emission of the socket only from the app.js, and emit nodejs events from controllers, which will be captured in app.js and then issued by the socket.

app.js

const app = express();
const http = require('http');
const server = http.createServer(app);
const io = require('socket.io')(server);
const events = require('events');

io.on('connection', function(socket) {
    console.log("Someone has connected");
    var eventEmitter = new events.EventEmitter();
    eventEmitter.on("newEvent", (msg) => { // occurs when an event is thrown
        socket.emit("news", msg);
    });

    exports.emitter = eventEmitter; // to use the same instance
});

myController.js

const app = require('../app');    
.....

if (app.emitter) // Checking that the event is being received, it is only received if there is someone connected to the socket
    app.emitter.emit("newEvent", "First task is done!");    
.....

if (app.emitter)
    app.emitter.emit("newEvent", "Second task is done!");

这篇关于在 Express.js 项目中使用 Socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆