我正在创建一个通用角度服务,它将执行信号器的本机操作
angular.module('signalr', []).provider('signalrService', [function () {
var configuration;
this.config = function (config) {
configuration = config;
};
this.$get = ['$log', '$timeout', '$q', '$rootScope', function ($log, $timeout, $q, $rootScope) {
return new signalrService(configuration, $log, $timeout, $q);
}];
}])
function signalrService(configuration, $log, $timeout, $q, $rootScope) {
if (!configuration) {
$log.log('Invalid config');
return;
}
var signalRhubs = $.hubConnection();
if (!signalRhubs) {
$log.log('SignalR is not referenced.');
return;
}
signalRhubs.logging = configuration.logging;
var warmMeUp = signalRhubs.createHubProxy('userHub');
warmMeUp.on('getOnlineUserList', function (data) {
console.log(data)
})
signalRhubs.connectionSlow(function () {
$log.warn('We are currently experiencing difficulties with the connection.')
});
signalRhubs.error(function (error) {
$log.error('SignalR error: ' + error)
});
$(signalRhubs).bind("onDisconnect", function (e, data) {
$timeout(function () {
$log.warn('Signalr Connection dead.')
}, 10000);
});
this.invoke = function (hubName, serverFunction, paramObject) {
var def = $q.defer();
var hubProxy = signalRhubs.createHubProxy(hubName)
signalRhubs.start().done(function () {
if (paramObject) {
hubProxy.invoke(serverFunction, paramObject).done(function (data) {
def.resolve(data);
}).fail(function (error) {
$log.error('SignalR error: ' + error)
def.reject();
});
}
else {
hubProxy.invoke(serverFunction).done(function (data) {
def.resolve(data);
}).fail(function (error) {
$log.error('SignalR error: ' + error)
def.reject();
});
}
}).fail(function () {
$log.error('Failed to start signalR')
def.reject();
});
return def.promise;
}
this.on = function (hubName,clientSubscribeFunction) {
var def = $q.defer();
var hubProxy = signalRhubs.createHubProxy(hubName)
hubProxy.on(clientSubscribeFunction, function (object) {
def.resolve(object);
});
return def.promise;
}
}
这是我的控制器
app.controller('homeController', ['$scope', 'signalrService', function ($scope, signalrService) {
signalrService.on('userHub', 'getOnlineUserList').then(function (userList) {
console.log(userList);
});
signalrService.invoke('userHub', 'getOnlineUsers')
}]);
这里的问题是当我取消注释我的" warmUp.on"服务代表,通用" on" (this.on)和" warmUp.on"被执行但当我评论" warmUp.on" (this.on)委托的通用也没有执行。 请帮助我解决问题
答案 0 :(得分:0)
更新:现在在github中托管https://github.com/pranaydutta89/angular-signalr-service
我不知道如何但这对我有用,我现在是第一个热身每个中心的一个功能,然后我的代表为我工作
angular.module('signalr', []).provider('signalrService', [function () {
var configuration;
this.config = function (config) {
configuration = config;
};
this.$get = ['$log', '$timeout', '$q', '$rootScope', function ($log, $timeout, $q, $rootScope) {
return new signalrService(configuration, $log, $timeout, $q);
}];
}])
function signalrService(configuration, $log, $timeout, $q, $rootScope) {
if (!configuration) {
$log.log('Invalid config');
return;
}
var signalRhubs = $.hubConnection();
if (!signalRhubs) {
$log.log('SignalR is not referenced.');
return;
}
signalRhubs.logging = configuration.logging;
angular.forEach(configuration.register, function (key,value) {
var x = signalRhubs.createHubProxy(key.hub);
x.on(key.method, function (data) {
console.log(data)
});
})
signalRhubs.connectionSlow(function () {
$log.warn('We are currently experiencing difficulties with the connection.')
});
signalRhubs.error(function (error) {
$log.error('SignalR error: ' + error)
});
$(signalRhubs).bind("onDisconnect", function (e, data) {
$timeout(function () {
$log.warn('Signalr Connection dead.')
}, 10000);
});
this.invoke = function (hubName, serverFunction, paramObject) {
var def = $q.defer();
var hubProxy = signalRhubs.createHubProxy(hubName)
signalRhubs.start().done(function () {
if (paramObject) {
hubProxy.invoke(serverFunction, paramObject).done(function (data) {
def.resolve(data);
}).fail(function (error) {
$log.error('SignalR error: ' + error)
def.reject();
});
}
else {
hubProxy.invoke(serverFunction).done(function (data) {
def.resolve(data);
}).fail(function (error) {
$log.error('SignalR error: ' + error)
def.reject();
});
}
}).fail(function () {
$log.error('Failed to start signalR')
def.reject();
});
return def.promise;
}
this.on = function (hubName,clientSubscribeFunction) {
var def = $q.defer();
var hubProxy = signalRhubs.createHubProxy(hubName)
hubProxy.on(clientSubscribeFunction, function (object) {
def.resolve(object);
});
return def.promise;
}
}
@angular config
.config(['signalrServiceProvider', function (signalrProvider) {
//register single function of each hub to warm him up
signalrProvider.config({
logging: true,
register: [{
hub: 'warmHub',
method : 'hello'
}, {
hub: 'userHub',
method: 'method2client'
}],
})
}]);