为什么socket.io为一个事件返回多次

我刚开始拿起socket.io。 我试图在angular.js中使用它,一切正常。 但是,它多次不断返回

在我的controller.js

socketio.emit('GameOver',$scope.currentPlayer.Name); socketio.on('GameOverEmit',function(data){ if(data === $rootScope.user.user_name){ var result = { opponent : rdyplayers.user_name, result : "won" }; // keep getting multiple result console.log(result); }else{ var result = { opponent : rdyplayers.user_name, result : "lose" }; // keep getting multiple result console.log(result); } }); 

在我的server.js

 io.sockets.on('connection', function (client) {​ client.user_name = user.user_name;​ //Useful to know when someone connects console.log('\t socket.io:: player ' + client.user_name + ' connected'); // playerturn client.on('PlayerTurn',function(data){ io.emit('PlayerTurnEmit',data);​ }); // game over client.on('GameOver',function(data){ io.emit('GameOverEmit',data);​ }); // button disabled client.on('PlayerButtonDisabled',function(data){ io.emit('PlayerButtonDisabledEmit',data); }); //When this client disconnects client.on('disconnect', function () { //Useful to know when someone disconnects console.log('\t socket.io:: client disconnected ' + client.user_name ); io.emit("disconnected",client.user_name); }); //client.on disconnect​ }); //io.sockets.on connection  io.sockets.on('connection', function (client) {​ client.user_name = user.user_name;​ //Useful to know when someone connects console.log('\t socket.io:: player ' + client.user_name + ' connected'); // playerturn client.on('PlayerTurn',function(data){ io.emit('PlayerTurnEmit',data);​ }); // game over client.on('GameOver',function(data){ io.emit('GameOverEmit',data);​ }); // button disabled client.on('PlayerButtonDisabled',function(data){ io.emit('PlayerButtonDisabledEmit',data); }); //When this client disconnects client.on('disconnect', function () { //Useful to know when someone disconnects console.log('\t socket.io:: client disconnected ' + client.user_name ); io.emit("disconnected",client.user_name); }); //client.on disconnect​ }); //io.sockets.on connection  io.sockets.on('connection', function (client) {​ client.user_name = user.user_name;​ //Useful to know when someone connects console.log('\t socket.io:: player ' + client.user_name + ' connected'); // playerturn client.on('PlayerTurn',function(data){ io.emit('PlayerTurnEmit',data);​ }); // game over client.on('GameOver',function(data){ io.emit('GameOverEmit',data);​ }); // button disabled client.on('PlayerButtonDisabled',function(data){ io.emit('PlayerButtonDisabledEmit',data); }); //When this client disconnects client.on('disconnect', function () { //Useful to know when someone disconnects console.log('\t socket.io:: client disconnected ' + client.user_name ); io.emit("disconnected",client.user_name); }); //client.on disconnect​ }); //io.sockets.on connection  io.sockets.on('connection', function (client) {​ client.user_name = user.user_name;​ //Useful to know when someone connects console.log('\t socket.io:: player ' + client.user_name + ' connected'); // playerturn client.on('PlayerTurn',function(data){ io.emit('PlayerTurnEmit',data);​ }); // game over client.on('GameOver',function(data){ io.emit('GameOverEmit',data);​ }); // button disabled client.on('PlayerButtonDisabled',function(data){ io.emit('PlayerButtonDisabledEmit',data); }); //When this client disconnects client.on('disconnect', function () { //Useful to know when someone disconnects console.log('\t socket.io:: client disconnected ' + client.user_name ); io.emit("disconnected",client.user_name); }); //client.on disconnect​ }); //io.sockets.on connection  io.sockets.on('connection', function (client) {​ client.user_name = user.user_name;​ //Useful to know when someone connects console.log('\t socket.io:: player ' + client.user_name + ' connected'); // playerturn client.on('PlayerTurn',function(data){ io.emit('PlayerTurnEmit',data);​ }); // game over client.on('GameOver',function(data){ io.emit('GameOverEmit',data);​ }); // button disabled client.on('PlayerButtonDisabled',function(data){ io.emit('PlayerButtonDisabledEmit',data); }); //When this client disconnects client.on('disconnect', function () { //Useful to know when someone disconnects console.log('\t socket.io:: client disconnected ' + client.user_name ); io.emit("disconnected",client.user_name); }); //client.on disconnect​ }); //io.sockets.on connection  io.sockets.on('connection', function (client) {​ client.user_name = user.user_name;​ //Useful to know when someone connects console.log('\t socket.io:: player ' + client.user_name + ' connected'); // playerturn client.on('PlayerTurn',function(data){ io.emit('PlayerTurnEmit',data);​ }); // game over client.on('GameOver',function(data){ io.emit('GameOverEmit',data);​ }); // button disabled client.on('PlayerButtonDisabled',function(data){ io.emit('PlayerButtonDisabledEmit',data); }); //When this client disconnects client.on('disconnect', function () { //Useful to know when someone disconnects console.log('\t socket.io:: client disconnected ' + client.user_name ); io.emit("disconnected",client.user_name); }); //client.on disconnect​ }); //io.sockets.on connection 

我是否正确使用socket.io? 为了将数据广播给大家,我只是把数据传递到emit ,然后把数据通过数据传递给服务器,然后把数据传回到前端?

对于广播使用socket.broadcast.emit()

我没有发现任何套接字断开监听事件代码在您的客户端代码..尝试添加socketio.disconnect客户端断开连接

在客户端添加一个侦听器,从服务器端到客户端,通过添加socketio.on('disconnect', function(){do something})

它由controller.js引起的,每当我重新访问页面,它将build立一个新的套接字,所以我做了每当用户离开或closures选项卡它将删除所有的监听器。 我最终通过在我的service.js中添加getsocket来解决这个问题

 ttt.factory('socketio', ['$rootScope', function ($rootScope) { 'use strict'; var socket = io.connect(); return { on: function (eventName, callback) { socket.on(eventName, function () { var args = arguments; $rootScope.$apply(function () { callback.apply(socket, args); }); }); }, emit: function (eventName, data, callback) { socket.emit(eventName, data, function () { var args = arguments; $rootScope.$apply(function () { if (callback) { callback.apply(socket, args); } }); }); }, getSocket: function() { return socket; } }; }]); 

并在我的controller.js我做的

 $scope.$on('$destroy', function (event) { socketio.getSocket().removeAllListeners(); });