在文件之间从socket

在文件之间从socket

本文介绍了在文件之间从socket.io共享服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图能够从其他文件中调用io.重点是,当用户进入房间或​​调用 io.sockets 等时,它不会更新.

I'm trying to be able to call io from other files. Point is that it doesn't update when a user is put into a room, or when io.sockets is called etc.

server.js

var options = {
  key: fs.readFileSync('./certs/file.key'),
  cert: fs.readFileSync('./certs/file.crt'),
  requestCert: false
};
var app = express();
var server = require('https').createServer(options, app);
var io = require('socket.io').listen(server); io.origins('*:*');
global.io = io;

io.on('connection', function(socket) {  .. }

我也尝试过;

module.exports.io

,然后从另一个文件

require('server.js').io

这也不起作用,我想在一个文件中运行服务器js,即服务器js,我在这里处理每个传入的套接字,等等.这是我尝试过的两件事,但是它们都导致相同的结果问题.

This didn't work either, I want to be running my server in one file which is the server js, I handle every incoming socket here etc. These are the two things I've tried but they both result in the same issue.

推荐答案

有很多不同的方案可以与其他模块共享中心变量(例如 io 变量).究竟有多大的意义取决于整体架构,您希望模块如何可恢复使用,等等.但是,所有方法都使用导入和导出的某种组合来在模块之间共享数据,而无需使用 global

There are many different schemes for sharing a central variable (like your io variable) with other modules. How exactly makes sense to do it depends upon an overall architecture, how you want your modules to be resuable, etc... but all use some combination of importing and exporting to share data between modules without using global.

在您的特定情况下,您可以做一些非常简单的事情:

In your specific case, you can do something very simple:

server.js

const options = {
  key: fs.readFileSync('./certs/file.key'),
  cert: fs.readFileSync('./certs/file.crt'),
  requestCert: false
};
const app = express();
const server = require('https').createServer(options, app);
const io = require('./socket.js').init(server);

io.on('connection', function(socket) {  .. }

socket.js

let io;
module.exports = {
    init: function(server) {
        // start socket.io server and cache io value
        io = require('socket.io').listen(server); io.origins('*:*');
        return io;
    }
    getio: function() {
        // return previously cached value
        if (!io) {
            throw new Error("must call .init(server) before you can call .getio()");
        }
        return io;
    }
}

在其他想要访问io的模块中

const io = require('./socket.js').getio();

在这里不必说,在调用 .getio()之前,必须先调用 .init(server).这利用了node.js模块缓存系统的优势,因此,每次调用 require('./socket.js')时,它都会返回给您第一次加载的模块,因此您有了访问以前缓存的 io 实例.

It should go without saying here that you have to call .init(server) before you can call .getio(). This takes advantage of the node.js module caching system so that each time you call require('./socket.js') it is returning to you the same module that was first loaded and thus you have access to the previously cached io instance.

仅供参考,这被称为拉"模型,其中想要访问其他内容的模块使用 require()语句将拉"到所需的变量中.

FYI, this is called a "pull" model where a module that wants access to something else uses a require() statement to "pull" in the variable it wants.

还有一个推入模块,在该模块中,模块的加载器在加载模块后通过调用该模块中的函数将数据推入模块.

There is also a push module where the loader of the module pushes the data to the module by calling a function in that module after it loads the module.

还有其他一些方法可以做到:

Here are some other ways to do it:

从app.js导出

您必须注意此方案的循环依赖关系,因为如果app.js执行 require('./a.js'),然后 a.js 执行require('./app.js'),您可以创建一个循环依赖项,该依赖项将失败.因此,只有在模块加载后(例如在模块构造函数中) a.js 正在执行 require('./app.js')`时,此模型才有效.

You have to watch out for circular dependencies with this scheme because if app.js does require('./a.js') and then a.js does require('./app.js'), you can create a circular dependency which will fail. So, this model only works ifa.jsis doing arequire('./app.js')` after module load (like in a module constructor).

app.js

const options = {
  key: fs.readFileSync('./certs/file.key'),
  cert: fs.readFileSync('./certs/file.crt'),
  requestCert: false
};
const app = express();
const server = require('https').createServer(options, app);
const io = require('socket.io').listen(server); io.origins('*:*');


io.on('connection', function(socket) {  .. }

// exports some things we want to share
module.exports  = {
  io: io,
  app: app
};

一些其他想要访问io的文件

 // module constructor
 modules.exports = function() {
     // can use io in here
     const io = require('./app.js').io;
}


推送模型

在此模块中,您将 io 变量传递给加载该模块时需要它的任何其他模块.

In this module, you pass the io variable to any other module that needs it when you load that other module.

app.js

const options = {
  key: fs.readFileSync('./certs/file.key'),
  cert: fs.readFileSync('./certs/file.crt'),
  requestCert: false
};
const app = express();
const server = require('https').createServer(options, app);
const io = require('socket.io').listen(server); io.origins('*:*');


io.on('connection', function(socket) {  .. }

// load someotherfile.js and pass it the io variable
require('./someotherfile.js')(io);

一些其他想要访问io的文件

module.exports = function(io) {
    // put whatever code for your module here
    setInterval(function() {
        io.emit(...);
    }, 1000);
}

这篇关于在文件之间从socket.io共享服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 14:29