【问题标题】:SignalR: Connection has not been fully initialized. Use .start().done() or .start().fail() to run logic after the connection has startedSignalR:连接尚未完全初始化。连接开始后使用 .start().done() 或 .start().fail() 运行逻辑
【发布时间】:2017-02-06 18:46:39
【问题描述】:

尝试复制一个示例时,我遇到了从服务器到我的计算机的连接未建立的问题,但是如果可行,则当我远程工作时。

链接示例

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

});

【问题讨论】:

  • 你有解决办法吗?

标签: javascript c# angularjs signalr


【解决方案1】:

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

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

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

【讨论】:

  • 我稍后再试,但是当不使用jsonp: true时,会显示一些错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 2011-07-22
  • 2012-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多