只接受socket.io中客户端发出的第一个事件并丢弃重复事件

我是一个新的node.js和socket.io。 我正在创build一个Web应用程序,其中将有数百个客户等待投标产品。

我们已经使用jquery定时器,当这个定时器为零时,所有的客户端发出拍卖closures事件到服务器。

现在我所需要的只有一个(第一个)产品closures事件被nodejs服务器(app.js)接受,其余的需要被丢弃。

//client requesting server to close the product socket.emit('time_finished', {'id':id,'name':name,'time':time}); //and my server code (receive time finished event from client) socket.on('time_finished',function(data) { //performing product closing operation here } 

任何帮助将是非常可观的。

如果您只有一个产品,则可以在第一个事件到达之后将布尔值设置为true。

  var alreadySent = false; socket.on('time_finished',function(data) { if(!alreadySent){ alreadySent = true; //performing product closing operation here } } 

但是如果您有许多产品,则可以将状态保存在数组或外部数据库中。

  var alreadySentIds = []; socket.on('time_finished',function(data) { if(alreadySentIds.indexOf(data.productId) == -1){ // not existing alreadySentIds.push(data.productId); //performing product closing operation here } }