【问题标题】:Angularjs SignalR connection not workingAngularjs SignalR连接不起作用
【发布时间】:2017-12-02 08:22:52
【问题描述】:

出于某种原因,这应该可行。感觉好像我错过了什么,但我不确定。我可以在 jquery 端返回数据。在 Angularjs 方面,我可以发送到服务器,但客户端似乎没有启动。我还在 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();





    }

【问题讨论】:

    标签: angularjs signalr


    【解决方案1】:

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

    // 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!!')
    
        }
    
    
    
    });
    

    如果你能做到这一点,请告诉我,否则我将分享一个样本 围绕它进行项目。

    【讨论】:

    • 连接成功。在我发布数据之后。我不能得到任何回报。我添加了控制台,但它永远不会让它通过 client.reloaddata。真令人沮丧。它应该工作,但没有。我有 Signalr url 只是无法通过客户端。我在我试过的上面添加了snipplet。
    • @/sevensnake77787 更新了答案,如果你能做到这一点,请告诉我,否则我将围绕它分享一个示例项目。
    猜你喜欢
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 2015-08-09
    • 1970-01-01
    相关资源
    最近更新 更多