简体   繁体   English

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

[英]SignalR: Connection has not been fully initialized. Use .start().done() or .start().fail() to run logic after the connection has started

Trying to replicate an example I have encountered the problem that the connection is not made, when it comes to do it from a server to my computer, but when I work remotely if it works.尝试复制一个示例时,我遇到了无法建立连接的问题,当涉及到从服务器到我的计算机时,但是当我远程工作时,如果它有效。

Links example链接示例

link 1 link 2 链接 1 链接 2

This is my code这是我的代码

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

});

You should only have one connection.start() and not two.您应该只有一个connection.start()而不是两个。 You need to add the done() and fail() into that call.您需要将done()fail()到该调用中。

connection.start({ ... }).done(function() {...}).fail(function() {...})

Otherwise you'll try to start it twice.否则,您将尝试启动它两次。 It might seem to work locally since there is no delay but in actual conditions the first won't finish before the second.它似乎在本地工作,因为没有延迟,但在实际情况下,第一个不会在第二个之前完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM