gpt4 book ai didi

.net - SignalR 和 AngularJS

转载 作者:行者123 更新时间:2023-12-01 19:39:00 26 4
gpt4 key购买 nike

我在我的应用程序中使用 SignalR,它可以使用简单的 javascript,但现在我尝试使用 Angular-wrapper https://github.com/JustMaier/angular-signalr-hub 重写它。 ,但我在尝试连接到我的集线器时遇到了错误:

Error: SignalR: Connection must be started before data can be sent. Call .start() before .send()
at Object.signalR.fn.signalR.send (http://localhost:47124/Scripts/jquery.signalR-2.1.1.js:789:23)
at Object.hubProxy.fn.hubProxy.invoke (http://localhost:47124/Scripts/jquery.signalR-2.1.1.js:2609:24)
at Hub.invoke (http://localhost:47124/Scripts/angular-signalr-hub.js:30:28)
at Hub.(anonymous function) [as loadRecentPhotos] (http://localhost:47124/Scripts/angular-signalr-hub.js:49:24)
at Object.PhotoMarkers.load (http://localhost:47124/Scripts/AngApp.js:47:17)
at http://localhost:47124/Scripts/angular.js:10836:21
at Scope.$eval (http://localhost:47124/Scripts/angular.js:12673:28)
at pre (http://localhost:47124/Scripts/angular.js:19943:15)
at nodeLinkFn (http://localhost:47124/Scripts/angular.js:6684:13)
at compositeLinkFn (http://localhost:47124/Scripts/angular.js:6098:13) <div class="modal-body" ng-init="markers.load()">

这是我的代码:

angular.module('FlickrMaps', ['SignalR'])
.factory('PhotoMarkers', ['$rootScope', 'Hub', function ($rootScope, Hub) {
var PhotoMarkers = this;

//Hub setup
var hub = new Hub('photos', {
listeners: {
'addTotalPhotosCount': function (total) {
PhotoMarkers.totalCount = total;
$rootScope.$apply();
},

'initPhotoMarker': function (photo) {
var photolocation = new window.google.maps.LatLng(photo.Latitude, photo.Longitude);
PhotoMarkers.all.push(new window.google.maps.Marker({
position: photolocation,
title: photo.Title,
photoThumb: photo.PhotoThumbScr,
photoOriginal: photo.PhotoOriginalScr
}));
$rootScope.$apply();
},

'photosProcessed': function () {
PhotoMarkers.processedPhotosCount++;
$rootScope.$apply();
},
},
methods: ['loadRecentPhotos'],
errorHandler: function (error) {
console.error(error);
}
});

//Variables
PhotoMarkers.all = [];
PhotoMarkers.processedPhotosCount = 0;
PhotoMarkers.totalCount = 0;

//Methods
PhotoMarkers.load = function () {
$('#myModal').modal({
backdrop: 'static',
keyboard: false
});
hub.loadRecentPhotos();
};

return PhotoMarkers;
}])
.controller('MapController', ['$scope', 'PhotoMarkers', function ($scope, PhotoMarkers) {
$scope.markers = PhotoMarkers;
}])

有人了解吗?

最佳答案

您一定遇到了异步问题。当您调用方法时,连接尚未打开。

尝试修复您的signalr-hub.js,我认为我们应该使用不同的 Promise 来停止和启动。

    Hub.disconnect = function () {
Hub.stopPromise = Hub.connection.stop();
};
Hub.connect = function () {
Hub.startPromise = Hub.connection.start();
};

还有:

angular.forEach(options.methods, function (method) {
Hub[method] = function () {
var args = $.makeArray(arguments);
args.unshift(method);
return Hub.startPromise.then(function(){
//invoke method only after startPromise is resolved
return Hub.invoke.apply(Hub, args);
}
};
});

或者:

angular.forEach(options.methods, function (method) {
Hub[method] = function () {
var args = $.makeArray(arguments);
args.unshift(method);

var def = $q.def();
Hub.startPromise.then(function(){
//invoke method only after startPromise is resolved
Hub.invoke.apply(Hub, args).done(function(){
def.resolve();
});
}

return def.promise;
};
});

不要忘记将 $q 注入(inject)您的 Hub。这会返回一个 Angular Promise,而不是 jQuery Promise。因此,Angular 知道该事件,我们不需要在回调中使用 $rootScope.$apply()

还有:

//Adding additional property of promise allows to access it in rest of the application.
Hub.startPromise = Hub.connection.start();

关于.net - SignalR 和 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25787449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com