2017-01-09 73 views
1

我使用SignalR与angularjs(1.5.6)angularjs 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); 

} 

在此先感谢

回答

0

我用它来修复它:

'use strict';

app.factory('commonFactory',commonFactory);

commonFactory $注入= [ '$ rootScope', '中心', '$日志']。

功能commonFactory($ rootScope,集线器,$日志){ VAR范围= $ rootScope;

变种实例= { };

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; }