Socket.io发射两次

我在我的Node.js后端创build了一个socket.io,它发出了AngularJS然后捕获并显示错误的某些事件,并更改了一些$ rootScopevariables。 我有一个问题,这个发射似乎每次都给我两次通知,而且我不知道问题出在哪里

server.js (shortened)

 var socket = require('socket.io'); var app = express(); var router = express.Router(); app.use('/api', router); var server = app.listen(config.conn_port, function(){ console.log("Server launched at port " + config.conn_port); }); var io = socket.listen(server); require('./api/routes')(router, connection, io); 

routes.js

 module.exports = function(router, connection, socket) { # Net event client.on('error', function () { socket.emit('serverDisconnected', {message: 'Connection ended', server_conn: false}); }); } 

app.js (AngularJS)

 app.factory('socket', function ($rootScope) { 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); } }); }) } }; }); 

AngularJS Controller

 dashboardModule.controller('dashboardMain', function ($scope, $http, $rootScope, Notification, socket) { socket.on('serverDisconnected', function (message) { Notification.error(message.message); }); }); 

       

网上收集的解决方案 "Socket.io发射两次"