繁体   English   中英

我可以将 socket.io 事件侦听器分成不同的模块吗?

[英]Can I separate socket.io event listeners into different modules?

我正在处理超过 15 个不同的套接字事件,我想在与这些事件相关的模块中管理某些 socket.io 事件。

例如,我想让一个名为 login.js 的文件处理login套接字事件,一个名为 register.js 的文件处理注册套接字事件。

索引.js:

socket.on("connection", function (client) {

    console.log("Client connected to socket!");

    client.on("login", function (data) {

        validate(data){

            socket.sockets.emit("login_success", data);

        }

    });

    client.on("register", function (data) {

        register(data){

            socket.sockets.emit("register_success", data);

        }

    });

});

有没有一种方法可以将client.on("register", function (data) { ...放在一个文件中,并将client.on("login", function (data) { ...放在另一个文件中?

我通常将各种与客户端相关的功能(我通常称它们为处理程序)拆分为单独的模块,然后在创建 socket.io 连接的任何文件中require和使用它们。

这是一个示例模块,它导出一个期望传递给 socket.io 客户端的函数:

/* register-handler.js */
module.exports = function (client) {
  // registration related behaviour goes here...
  client.on('register', function (data) {
    // do stuff
  });
};

它由创建新套接字的文件使用,侦听连接,并将它们传递给处理程序,然后处理程序侦听客户端上的事件。

/*  main.js */
// require your handlers
var handleRegister = require('./register-handler');

// .. set up socket.io

socket.on('connection', function (client) {
  // register handlers
  handleRegister(client);
});

这是一种方法

socket.on("connection", function (client) {

    console.log("Client connected to socket!");

    require('./login')(socket, client);
    require('./register')(socket, client);
});

登录.js

module.exports = function(socket, client) {
    client.on("login", function (data) {

        validate(data){

            socket.sockets.emit("login_success", data);

        }

    });
};

创建一个侦听器文件夹,将所有事件处理程序放在单独的文件中,然后通过一些技巧您可以包含所有这些侦听器。

在监听器/index.js 中:

 module.exports = (io) => {
 const fs = require('fs');
 const path = require('path');
 const listenersPath = path.resolve(__dirname);
 io.on('connection', (socket) => {
    fs.readdir(listenersPath, (err, files) => {
      if (err) {
        process.exit(1);
      }
      files.map((fileName) => {
        if (fileName !== 'index.js') {
          require(path.resolve(__dirname, fileName))(io, socket);
        }
      });
    });
  });
};

然后在不同的文件中,您可以处理侦听器。

侦听器/登录.js:

  module.exports = (io, socket) => {
      socket.on('login', (data) => {
        // handle the event
      });
  }

在您的 app.js 文件中:

const initListeners = require('./listeners/index');

const server = app.listen(3000);
const io = require('socket.io')(server);
initListeners(io);

您也可以为事件发射器执行此操作。

是的,您可以使用exportsrequire

看看这个

这很简单

// export from your handler file:

exports.myHandler = socket => data => { // the socket object is available due to closure }

// import in your socket app.js file
 
import { myHandler } from './handlers/mySocketHandler'
 
// now just call the myHandler function.
// pass the socket object from the app.js file and it will return a handler function

socket.on('message', myHandler(socket))

暂无
暂无

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

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