如何将socket.io导出到nodejs中的其他模块? [英] How can i export socket.io into other modules in nodejs?

查看:155
本文介绍了如何将socket.io导出到nodejs中的其他模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 socket.io app.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

然后,在消费者中。 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

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.

这篇关于如何将socket.io导出到nodejs中的其他模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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