angularjs signalr - 解析服务器方法

时间:2017-01-09 23:24:26

标签: angularjs signalr

使用带有angularjs(1.5.6)的SignalR时遇到一些困难

我使用以下库: Https://github.com/JustMaier/angular-signalr-hub

我有以下必须支持SignalR的工厂。

'use strict';
app.factory('commonFactory', commonFactory);

commonFactory.$inject = ['$rootScope', 'Hub', '$log'];

function commonFactory($rootScope, Hub, $log) {
    var scope = $rootScope;

    scope.hub = {
        instance : {},
        isReady : false
    };

    var _listeners = {
        'login': function (message) {
            $log.log(message);
            $rootScope.$apply();
        }
    };
    var _methods = ['connect', 'sendMessage'];

    var _errorHandler = function (error) {
        console.error(error);
    };

    var _hub = new Hub('message', { methods: [] })
    .promise.done(function () {
        _hub.listeners = _listeners;
        _hub.methods = _methods;
        _hub.errorHandler = _errorHandler;

        scope.hub.isReady = true;
        scope.hub.instance = _hub;
    }); 
}

在我的控制器中,我使用我的工厂:

app.controller('loginController', loginController);

loginController.$inject = ['$scope', 'commonFactory'];

function loginController($scope, commonFactory) {
    $scope.login = _login;


    $scope.model = {
        user : {
            id : 1
        }
    }

    function _login() {
        commonFactory.hub.instance.connect($scope.model.user.id);
    }
}

以下行有误: commonFactory.hub.instance.connect($ scope.model.user.id);

它说:

  

commonFactory.hub.instance.connect不是函数

如何解析服务器方法(用c#编写)

界面:

public interface IMessageHub
{
    [HubMethodName("login")]
    void Login(object message);

    [HubMethodName("send")]
    void Send(object message);

    [HubMethodName("connect")]
    void Connect(Guid userGuid);

}

提前致谢

1 个答案:

答案 0 :(得分:0)

我已经修好了:

  

'使用严格的';

     

app.factory(' commonFactory',commonFactory);

     

commonFactory。$ inject = [' $ rootScope',' Hub',' $ log'];

     

function commonFactory($ rootScope,Hub,$ log){       var scope = $ rootScope;

     

var instance = {       };

var hub = new Hub('message', {
    listeners: {
        'login': function (from, to, message) {
          $log.log(message);
          $rootScope.$apply();;
        }
    },

    //server side methods
    methods: ['connect', 'sendMessage'],

    //handle connection error
    errorHandler: function (error) {
        console.error(error);
    }
});

var _login = function (userGuid) {
    hub.promise.done(function () {
        hub.login(userGuid); //Calling a server method
    });
};

instance.login = _login;      return instance; }