除了Socket.io中的特定事件侦听器之外,删除所有事件侦听器

我有socket.io的node.js应用程序,用于实时select和加载不同的外部模块(我称之为“活动”)。

由于每个模块都将自己的事件绑定到套接字,所以当我从一个模块更改为另一个模块时,我希望能够从套接字中删除前一个模块添加的所有事件侦听器。

我会使用emitter.removeAllListeners() ,但是这也将删除我在服务器中定义的事件,这是我不想要的。

以下是我的代码的样子:

app.js

 // Boilerplate and some other code var currentActivity; io.sockets.on('connection', function(client){ client.on('event1', callback1); client.on('event2', callback2); client.on('changeActivity', function(activityPath){ var Activity = require(activityPath); currentActivity = new Activity(); // Here I'd like some loop over all clients and: // 1.- Remove all event listeners added by the previous activity // 2.- Call currentActivity.bind(aClient) for each client }); }) 

一个示例活动将如下所示

someActivity.js

 module.exports = function(){ // some logic and/or attributes var bind = function(client){ client.on('act1' , function(params1){ // some logic }); client.on('act2' , function(params2){ // some logic }); // etc. } } 

所以,例如在这个例子中,如果我从someActivity.js更改为其他活动,我希望能够为所有客户端删除“act1”和“act2”的侦听器,而不删除“event1 “,”event2“和”changeActivity“。

任何想法如何做到这一点?

我会在每个模块中创build一个名为unbind的方法,删除bind函数添加的所有监听器:

 var fun1 = function(params1){ // some logic }; var fun2 = function(params2){ // some logic }; module.exports = function(){ // some logic and/or attributes var bind = function(client){ client.on('act1' , fun1); client.on('act2' , fun2); } var unbind = function(client){ client.removeEventListener('act1',fun1); client.removeEventListener('act2',fun2); }; }; 

如果您需要访问侦听器中的客户端 ,我会重构它以将客户端传递给构造函数:

 function MyModule(client){ this.client = client; }; MyModule.prototype.fun1 = function(params1){ //do something with this.client }; MyModule.prototype.fun2 = function(params2){ //do something with this.client }; MyModule.prototype.bind = function(){ this.client.on('act1' , this.fun1); this.client.on('act2' , this.fun2); }; MyModule.prototype.unbind = function(){ this.client.removeEventListener('act1' , this.fun1); this.client.removeEventListener('act2' , this.fun2); }; module.exports = MyModule; 

那么你可以像这样使用它:

 client.on('changeActivity', function(activityPath){ var Activity = require(activityPath); var currentActivity = activityCache[activityPath] || new Activity(client); //use the existing activity or create if needed previousActivity.unbind(); currentActivity.bind(); });