我正在创建一个依赖socket.io进行实时通知的应用。发布redis消息时会触发一个侦听器。这个redis监听器需要是全局的,并且数据被推送到提供者以在控制器之间共享它。所以,我正在做的是创建一个通知提供程序并注入套接字提供程序以设置监听器。但是,即使定义了socke提供程序,on()也不是。有没有办法可以在应用程序配置期间完成此任务?或者我只是在这里拍摄?感谢
var CRM = angular.module('CRM', ['ngRoute', 'ngResource']);
CRM.config(['socketProvider',function(socket) {
socket.connect();
}]);
CRM.config(['notificationsProvider',function(not) {
not.listen();
}]);
CRM.provider('socket', function() {
var socket;
return {
connect: function() {
// var host = ''; // Production
var host = 'http://localhost:3000'; // Dev
socket = io.connect(host);
var client = {
clientType: 'Browser',
registerFor: ['peberry.*']
};
socket.emit('register', client);
},
$get: function($rootScope) {
return {
on: function(event, callback) {
socket.on(event, function() {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
},
emit: function(event, data, callback) {
socket.emit(event, data, function() {
var args = aguments;
$rootScope.$apply(function() {
if (callback) {
callback.apply(socket, args);
}
});
});
}
}
}
}
});
CRM.provider('notifications',['socketProvider','redisMsgProvider', function(socket, redisMsg) {
var notifications = [];
return {
listen: function() {
console.log(socket.on)
socket.on('redis', function(msg) {
redisMsg.processRedis(msg);
var message = redisMsg.getMessage();
console.log(message);
});
},
$get: function() {
return {
getNotifications: function(action) {
if (notifications.length > 0) {
return notifications;
}
}
}
}
}
}]);