简体   繁体   English

分离套接字 IO 需要在 Node 和 Express 中使用更简洁的代码

[英]Separating socket IO calls for cleaner code in Node and Express

So, I'm making an app and I have gotten socket IO working and the app just runs on Express.所以,我正在制作一个应用程序,并且我已经让套接字 IO 工作,并且该应用程序只是在 Express 上运行。

Anyway, within the main server file is where I am doing my calls.无论如何,在主服务器文件中是我打电话的地方。 Some examples:一些例子:

//Object 1
io.of('/type1').on
(   'connection', socket => 
{   socket.on
    (   'call1', () =>
        {   doSomething1();
        }
    );
}
);

// Object 2
io.of('/type2').on
(   'connection', socket => 
{   socket.on
    (   'call2', () =>
        {   doSomething2();
        }
    );
}
);

Obviously, there are way more socket.on calls for the various objects.显然,对各种对象有更多的 socket.on 调用。 What I wanted to do was actually separate these objects' logic from the main loop into some separate files to better clean up the main server.js file I have.我想要做的实际上是将这些对象的逻辑从主循环中分离到一些单独的文件中,以更好地清理我拥有的主 server.js 文件。 I've got some 300 lines of various socket calls.我有大约 300 行各种套接字调用。

Something like socketCalls1.js for Object 1, etc. Object 1 等的 socketCalls1.js 之类的东西。

The only thing is that the io variable doesn't seem sharable across multiple files... Is there a good, clean way of doing this?唯一的问题是 io 变量似乎不能在多个文件之间共享......有没有一种好的、干净的方法来做到这一点?

The only thing is that the io variable doesn't seem sharable across multiple files...唯一的问题是 io 变量似乎无法在多个文件之间共享......

This is not true.这不是真的。 You can pass the io variable into functions and out of modules just like any other variable.您可以像任何其他变量一样将io变量传入函数和传出模块。 Also like any other variable it is automatically privately scoped to a single file.也像任何其他变量一样,它会自动私有地限定为单个文件。

There are several ways to share objects across files and if you've gotten this far I'm sure you've used most of them.有几种方法可以跨文件共享对象,如果您已经走到这一步,我相信您已经使用了其中的大部分。 Only maybe you may not have realized what you have been doing.只是也许你可能没有意识到你一直在做什么。

Method 1: pass io into functions.方法一:将io传入函数。

One way to split your logic is to split it across several modules.拆分逻辑的一种方法是将其拆分为多个模块。 Since you can pass objects into functions your modules should export functions.由于您可以将对象传递给函数,因此您的模块应该导出函数。 For example:例如:

  • file1: type1.js文件 1:type1.js

     function init (io) { io.of('/type1').on ( 'connection', socket => { socket.on ( 'call1', () => { doSomething1(); } ); } ); } module.exports = init;
  • file2: type2.js文件2:type2.js

     function init (io) { io.of('/type2').on ( 'connection', socket => { socket.on ( 'call2', () => { doSomething2(); } ); } ); } module.exports = init;
  • main.js main.js

     const type1 = require('./path/to/type1.js'); const type2 = require('./path/to/type2.js'); // some code to init your app... type1.init(io); // pass io to type1 module type2.init(io); // pass io to type2 module

This works because io is just a regular variable.这是因为io只是一个常规变量。 And just like any variable you can pass it as a function argument.就像任何变量一样,您可以将其作为 function 参数传递。 Nothing here is fancy, this is all basic features of programming languages.这里没有什么花哨的,这是编程语言的所有基本特征。

Method2: share io as a module.方法2:共享io作为一个模块。

Another technique is specific to how modules work in node.js.另一种技术特定于模块在 node.js 中的工作方式。 Modules are singletons in node.模块是节点中的单例。 What that means is that a module represents exactly one object.这意味着一个模块恰好代表一个 object。 If 3 different files require() s the module they will all get the same object.如果 3 个不同的文件require()是模块,它们都会得到相同的 object。

We can use this to share the io object between modules:我们可以使用它在模块之间共享io object:

  • file1: server-init.js文件 1:服务器-init.js

     let app = require('express')(); let http = require('http').Server(app); let io = require('socket.io')(http); module.exports = { app: app, http: http, io: io }
  • file2: type1.js文件2:type1.js

     const io = require('./path/to/server-init').io; // Alternatively you can use the syntax: // const { io } = require('./path/to/server-init'); function init () { io.of('/type1').on ( 'connection', socket => { socket.on ( 'call1', () => { doSomething1(); } ); } ); } module.exports = init;
  • file3: type2.js文件 3:type2.js

     const io = require('./path/to/server-init').io; function init () { io.of('/type2').on ( 'connection', socket => { socket.on ( 'call2', () => { doSomething2(); } ); } ); } module.exports = init;
  • main.js main.js

     const type1 = require('./path/to/type1.js'); const type2 = require('./path/to/type2.js'); const { http, app } = require('./path/to/server-init'); // some init code type1.init(); type2.init(); http.listen(PORT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM