admin管理员组

文章数量:1389772

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.

本文标签: