【发布时间】:2017-01-12 06:09:36
【问题描述】:
我在使用 SignalR 和 angularjs (1.5.6) 时遇到了一些困难
我使用以下库: 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);
}
提前致谢
【问题讨论】: