SignalR:连接尚未完全初始化.使用.start().done()或.start().fail()在连接启动后运行逻辑

Ped*_*les 3 javascript c# signalr angularjs

试图复制一个例子我遇到了没有建立连接的问题,当涉及到从服务器到我的计算机时,但是当我在远程工作时它是否正常工作.

链接示例

链接1 链接2

这是我的代码

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);

});
Run Code Online (Sandbox Code Playgroud)

Sam*_*nen 5

你应该只有一个connection.start()而不是两个.您需要将done()and 添加fail()到该调用中.

connection.start({ ... }).done(function() {...}).fail(function() {...})
Run Code Online (Sandbox Code Playgroud)

否则你会尝试两次启动它.它可能似乎在本地工作,因为没有延迟但在实际情况下第一个不会在第二个之前完成.