I'm handling over 15 different socket events, I'd like to manage certain socket.io events within the modules that are related to those events.
我正在处理超过15个不同的套接字事件,我想管理与这些事件相关的模块中的某些socket.io事件。
For example, I'd like to have a file named login.js handle the login
socket event, and a file named register.js handle the registration socket event.
例如,我想让一个名为login.js的文件处理登录套接字事件,一个名为register.js的文件处理注册套接字事件。
index.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);
}
});
});
Is there a way that I can put client.on("register", function (data) { ...
in one file and client.on("login", function (data) { ...
in another?
有没有办法可以将client.on(“register”,function(data){...放在一个文件中,而client.on(“login”,function(data){...放在另一个文件中?
3 个解决方案
#1
14
I usually split various client related functionality (I usually call them handlers) into individual modules, and then require
and use them in whatever file creates the socket.io connection.
我通常将各种与客户端相关的功能(我通常称之为处理程序)拆分为单独的模块,然后在任何创建socket.io连接的文件中要求并使用它们。
Here is an example module, that exports a function which expects to be passed a socket.io client:
这是一个示例模块,它导出一个期望传递socket.io客户端的函数:
/* register-handler.js */
module.exports = function (client) {
// registration related behaviour goes here...
client.on('register', function (data) {
// do stuff
});
};
Which is consumed by a file that creates a new socket, listens for connections, and passes them to the handler, which then listens to events on the client.
这是由创建新套接字的文件使用的,侦听连接,并将它们传递给处理程序,然后处理程序侦听客户端上的事件。
/* main.js */
// require your handlers
var handleRegister = require('./register-handler');
// .. set up socket.io
socket.on('connection', function (client) {
// register handlers
handleRegister(client);
});
#2
2
Here is one way
这是一种方式
socket.on("connection", function (client) {
console.log("Client connected to socket!");
require('./login')(socket, client);
require('./register')(socket, client);
});
login.js
module.exports = function(socket, client) {
client.on("login", function (data) {
validate(data){
socket.sockets.emit("login_success", data);
}
});
};
#1
14
I usually split various client related functionality (I usually call them handlers) into individual modules, and then require
and use them in whatever file creates the socket.io connection.
我通常将各种与客户端相关的功能(我通常称之为处理程序)拆分为单独的模块,然后在任何创建socket.io连接的文件中要求并使用它们。
Here is an example module, that exports a function which expects to be passed a socket.io client:
这是一个示例模块,它导出一个期望传递socket.io客户端的函数:
/* register-handler.js */
module.exports = function (client) {
// registration related behaviour goes here...
client.on('register', function (data) {
// do stuff
});
};
Which is consumed by a file that creates a new socket, listens for connections, and passes them to the handler, which then listens to events on the client.
这是由创建新套接字的文件使用的,侦听连接,并将它们传递给处理程序,然后处理程序侦听客户端上的事件。
/* main.js */
// require your handlers
var handleRegister = require('./register-handler');
// .. set up socket.io
socket.on('connection', function (client) {
// register handlers
handleRegister(client);
});
#2
2
Here is one way
这是一种方式
socket.on("connection", function (client) {
console.log("Client connected to socket!");
require('./login')(socket, client);
require('./register')(socket, client);
});
login.js
module.exports = function(socket, client) {
client.on("login", function (data) {
validate(data){
socket.sockets.emit("login_success", data);
}
});
};