最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SignalR: Connection has not been fully initialized. Use .start().done() or .start().fail() to run logic after the c

programmeradmin1浏览0评论

Trying to replicate an example I have encountered the problem that the connection is not made, when it es to do it from a server to my puter, but when I work remotely if it works.

Links example

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

});

Trying to replicate an example I have encountered the problem that the connection is not made, when it es to do it from a server to my puter, but when I work remotely if it works.

Links example

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

});
Share Improve this question asked Sep 28, 2016 at 16:51 Pedro Miguel Pimienta MoralesPedro Miguel Pimienta Morales 4533 gold badges12 silver badges32 bronze badges 1
  • have you got the solution? – King_Fisher Commented Dec 8, 2016 at 10:22
Add a ment  | 

1 Answer 1

Reset to default 5

You should only have one connection.start() and not two. You need to add the done() and fail() into that call.

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论