如何将 socket.io 导出到 nodejs 中的其他模块中?

2022-01-31 00:00:00 socket.io node.js javascript

我有 socket.ioapp.js 中工作,但是当我尝试从其他模块调用它时,它不会创建 io.connection 不确定?

I have socket.io working in app.js but when i am trying to call it from other modules its not creating io.connection not sure ?

app.js

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var ditconsumer = require('./app/consumers/ditconsumer');
ditconsumer.start(io);
server.listen(3000, function () {
    console.log('Example app listening on port 3000!');
});

consumer.js

consumer.js

module.exports = {
    start: function (io) {
        consumer.on('message', function (message) {
            logger.log('info', message.value);
            io.on('connection', function (socket) {
                socket.on('message', function(message) {
                    socket.emit('ditConsumer',message.value);
                    console.log('from console',message.value);
                });
            });
        });
}
}

推荐答案

由于 app.js 通常是您应用程序中的主要初始化模块,它通常会初始化 Web 服务器和 socket.io 并加载其他内容应用所需要的.

Since app.js is usually kind of the main initialization module in your app, it will typically both initialize the web server and socket.io and will load other things that are needed by the app.

因此,与其他模块共享 io 的典型方法是将它们传递给该模块的构造函数中的其他模块.这将像这样工作:

As such a typical way to share io with other modules is by passing them to the other modules in that module's constructor function. That would work like this:

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

// load consumer.js and pass it the socket.io object
require('./consumer.js')(io);

// other app.js code follows

然后,在 consumer.js 中:

Then, in consumer.js:

// define constructor function that gets `io` send to it
module.exports = function(io) {
    io.on('connection', function(socket) {
        socket.on('message', function(message) {
            logger.log('info',message.value);
            socket.emit('ditConsumer',message.value);
            console.log('from console',message.value);
        });
    });
};


或者,如果你想使用 .start() 方法来初始化事物,你可以用它做同样的事情(细微差别):


Or, if you want to use a .start() method to initialize things, you can do the same thing with that (minor differences):

// app.js
var server = require('http').createServer(app);
var io = require('socket.io')(server);

// load consumer.js and pass it the socket.io object
var consumer = require('./consumer.js');
consumer.start(io);

// other app.js code follows

以及consumer.js中的start方法

And the start method in consumer.js

// consumer.js
// define start method that gets `io` send to it
module.exports = {
    start: function(io) {
        io.on('connection', function(socket) {
            socket.on('message', function(message) {
                logger.log('info',message.value);
                socket.emit('ditConsumer',message.value);
                console.log('from console',message.value);
            });
        });
    };
}


这就是所谓的推".资源共享模块.加载你的模块通过在构造函数中传递一些共享信息给你.


This is what is known as the "push" module of resource sharing. The module that is loading you pushes some shared info to you by passing it in the constructor.

还有拉";模块本身调用其他模块中的方法来检索共享信息的模型(在本例中为 io 对象).

There are also "pull" models where the module itself calls a method in some other module to retrieve the shared info (in this case the io object).

通常,任一模型都可以工作,但考虑到模块的加载方式、谁拥有所需信息以及您打算如何在其他情况下重用模块,通常其中一个模型会感觉更自然.

Often, either model can be made to work, but usually one or the other will feel more natural given how modules are being loaded and who has the desired information and how you intend for modules to be reused in other circumstances.

相关文章