[英]SignalR: Connection has not been fully initialized. Use .start().done() or .start().fail() to run logic after the connection has started
尝试复制一个示例时,我遇到了无法建立连接的问题,当涉及到从服务器到我的计算机时,但是当我远程工作时,如果它有效。
链接示例
这是我的代码
var app = angular.module('app', []);
app.value('$', $);
app.factory('signalRSvc', function ($, $rootScope) {
return {
proxy: null,
initialize: function (acceptGreetCallback) {
//Getting the connection object
connection = $.hubConnection('http://190.109.185.138:8016');
//Creating proxy
this.proxy = connection.createHubProxy('HelloWorldHub');
//Starting connection
connection.start({ jsonp: true }).done(function () {
alert("funciono");
});
connection.start({ jsonp: true }).fail(function () {
alert("fallo");
});
//connection.start({ jsonp: true }).done(function () {
// console.log("connection started!");
//});
//Attaching a callback to handle acceptGreet client call
this.proxy.on('acceptGreet', function (message) {
$rootScope.$apply(function () {
acceptGreetCallback(message);
});
});
},
sendRequest: function (callback) {
//Invoking greetAll method defined in hub
this.proxy.invoke('greetAll');
}
}
});
app.controller('SignalRAngularCtrl', function ($scope, signalRSvc) {
$scope.text = "";
$scope.greetAll = function () {
signalRSvc.sendRequest();
}
updateGreetingMessage = function (text) {
$scope.text = text;
}
signalRSvc.initialize(updateGreetingMessage);
});
您应该只有一个connection.start()
而不是两个。 您需要将done()
和fail()
到该调用中。
connection.start({ ... }).done(function() {...}).fail(function() {...})
否则,您将尝试启动它两次。 它似乎在本地工作,因为没有延迟,但在实际情况下,第一个不会在第二个之前完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.