创建 angularjs + 信号器服务
creating angularjs + signalr service
我正在创建一个通用的 angular 服务,它将执行 signalr
的本机操作
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" 通用 on(this.on) 委托也未执行。
请帮我解决这个问题
更新:现在在 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(['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'
}],
})
}]);
我正在创建一个通用的 angular 服务,它将执行 signalr
的本机操作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" 通用 on(this.on) 委托也未执行。 请帮我解决这个问题
更新:现在在 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(['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'
}],
})
}]);