在文件之间从 socket.io 共享服务器 [英] Share server from socket.io between files
问题描述
我正在尝试能够从其他文件调用 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,我在这里处理每个传入的套接字等等.这是我尝试过的两件事,但它们的结果都是一样的问题.
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();
这里不用说,您必须先调用.init(server)
,然后才能调用.getio()
.这利用了 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 if
a.jsis doing a
require('./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 共享服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!