我通常将各种与客户端相关的功能(通常称为处理程序)拆分为各个模块,然后
require在创建socket.io连接的任何文件中使用它们。
这是一个示例模块,该模块导出一个期望传递给socket.io客户端的函数:
module.exports = function (client) { // registration related behaviour goes here... client.on('register', function (data) { // do stuff });};它由创建新套接字,侦听连接并将其传递给处理程序的文件使用,该处理程序随后侦听客户端上的事件。
// require your handlersvar handleRegister = require('./register-handler');// .. set up socket.iosocket.on('connection', function (client) { // register handlers handleRegister(client);});


