Angularjs SignalR连接无法正常工作

时间:2017-11-30 21:11:47

标签: angularjs signalr

出于某种原因,这应该有效。感觉像我错过了一些东西,但我不确定。我可以在jquery端返回数据。在Angularjs方面,我可以发送到服务器,但客户端似乎没有iniitate。我还测试了angularjs侧的连接,它似乎是初始化的。由于某种原因,我没有看到像jquery文件一样返回的数据。建议请。

Jquery在客户端上运行

   $(function () {
        $.connection.hub.start()
            .done(function () {
                console.log('SignalR connected, connection id = ' + $.connection.hub.id);
                console.log('Username = ' + '001');
                $.connection.messageHub.server.connectToMessageHub('001');
            })
            .fail(function (data) {
                console.log('SignalR failed to connect: ' + data);
            });
 
        $.connection.messageHub.client.reloadData = function (jsonObject) {
          console.log("data invokes called");
          console.log('get data=', jsonObject);
    
        };
     
    });
        $.connection.hub.start().done(function () { // initiate connection
      });
    });

AngularJS不起作用。我可以向服务器发送消息,但客户端似乎没有调用或启动。

    $scope.dataHub = null;
   $scope.dataHub = $.connection.messageHub;

        $.connection.hub.start(); // starts hub
// never makes it after start.
        $scope.dataHub.client.reloadData = function (message) {
            var newMessage = 'name' + ' says: ' + message;
            console.log('test', message);
 
            $scope.AllItems.push(newMessage);
            $scope.$apply();
        };

   $.connection.hub.logging = true;
    $.connection.hub.start().done(function () {
        console.log('Your Hub started Sucessfully.');
    });

    $scope.dataHub = function () {

        $scope.hub = $.connection.alertUser;

        $scope.notify = function () {
            console.log('loaded');
            $scope.hub.client.reloaddata = function (name) {
                console.log('loaded=', name);

                $scope.$apply(function () {
                    $scope.AllItems.push(name);
                });
            }
            $.connection.hub.start();
        }
        $scope.notify();





    }

1 个答案:

答案 0 :(得分:0)

  

我不确定您是否提供了正确的集线器网址,您可以参考下面的代码。   我在app.config中添加了以下代码,对我来说这很好,请查看你的

// create the module and name it signalRApp
var signalRApp = angular.module('signalRApp', ['ngRoute', 'ngMaterial', 'ngAnimate', 'timer']);
// configure our routes

$.connection.hub.url = 'http://localhost:44444/signalr';

hub = $.connection;

signalRApp.config(function ($routeProvider, $locationProvider) {
    $routeProvider
         // route for the home page
        .when('/', {
            templateUrl: 'app/template/index.html',
            controller: 'AlertCtrl'
        });


    $locationProvider.hashPrefix('');
});
// create the controller and inject Angular's $scope
//This should be your bootstraper of main controller of your application.
signalRApp.controller('mainController', function ($scope,$rootScope,$timeout) {
    // create a message to display in our view
    $rootScope.enableLoadingImage = false;
    // Pass  your hub name
    $rootScope.hub = hub.messageHub;

    $rootScope.Message = [];
    $scope.uniqueIds=[];

    $rootScope.hub.client.reloaddata  = function (item) {


        console.log('we got pointer here!!')

    }



});
  

请告诉我,如果你能做到这一点,我会分享一个样本   围绕它进行投射。