c# - SignalR.EventAggregatorProxy 简单示例问题

标签 c# signalr asp.net-web-api eventaggregator

我正在尝试使用 SignalR.EventAggregatorProxy 库,但我无法理解文档或演示。我不需要使用 Ninject,我只需要非常简单的 tes 应用程序,在服务器端使用 WEB API 和 Angular SPA。

这是我到目前为止所拥有的:

启动.cs

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();

        var proxy = new Lazy<IEventAggregator>(() => new Eventer());
        GlobalHost.DependencyResolver.Register(typeof(IEventAggregator), () => proxy.Value);

        app.MapEventProxy<Message>();
    }
}

消息类

public abstract class Message {}

public class MyMessage : Message
{
    public int Number { get; set; }
}

Eventer.cs 类触发事件

public class Eventer : IEventAggregator
{
    public void Subscribe(Action<object> handler)
    {
        Task.Factory.StartNew(() =>
        {
            int number = 0;

            while (true)
            {
                handler.Invoke(new MyMessage { Number = number++ });
                System.Threading.Thread.Sleep(1000);
            }
        });
    }
}

简单的app.js

(function () {
    'use strict';

    var app = angular.module("TestApp", ['signalR.eventAggregator']);

    app.controller("TestCtrl", ['$scope','$http', function ($scope, $http) {

        function onEvent(e) {
            console.log("event received => ", e);
        };

        $scope.eventAggregator().subscribe(EventAgrTest.Events.MyMessage, onEvent);

        $scope.headerText = "HEADER";
    }]);
})();

和index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="TestApp">
<head>
    <title></title>

    <script src="Scripts/jquery-1.6.4.js"></script>
    <script src="Scripts/jquery.signalR-2.2.0.js"></script>
    <script src="Scripts/jquery.signalR.eventAggregator-1.4.141.0.js"></script>
    <script src="/signalr/hubs"></script>
    <script src="/eventAggregation/events"></script>
</head>
<body ng-controller="TestCtrl">

    <h1>{{headerText}}</h1>

    <script src="Scripts/angular.js"></script>
    <script src="Scripts/jquery.signalR.eventAggregator.angular-1.4.143.0.js"></script>

    <script src="Scripts/app.js"></script>
</body>
</html>

当我运行应用程序时,我收到指向行的“TypeError:无法读取未定义的属性‘订阅’”错误

signalR.eventAggregator.subscribe(type, function(e) { ...

在 jquery.signalR.eventAggregator.angular-1.4.143.0.js

我确信我错过了一些东西,但我不确定是什么。

Wiki 中有一个名为“实现约束处理程序”( https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/wiki/Implement-constraint-handlers ) 的部分,它应该像从抽象类继承一样简单,但我不确定需要从此类继承什么。

console output

最佳答案

我是该库的作者,

它的设计初衷并不是使用简单的数据类型作为消息。

例如,您需要一个消息基类

public abstract class Message {}

创建实际消息

public class MyMessage : Message 
{
    public int Number { get;set; }
}

现在在配置更改中使用基类,这是一种让库知道哪些消息应该代理给 JavaScript 的方法

app.MapEventProxy<Message>();

在您的事件代理中更改为

handler.Invoke(new MyMessage { Number = number++ });

现在在您的客户端中更改为

$scope.eventAggregator().subscribe(EventAgrTest.Events.MyMessage, onEvent);

我应该将 Invoke 方法从采用类型对象更新为采用具有约束类的泛型类型

关于c# - SignalR.EventAggregatorProxy 简单示例问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33481538/

相关文章:

c# - 创建属性 setter 委托(delegate)

c# - 如何构造一个正确的MySQL连接串?

javascript - SignalR 2.2.0 和 SignalRjs 2.2.0 在声明连接 : Client version 1. 5 时失败,服务器版本 1.2

c# - signalR 调用服务器方法,该方法在集线器外部调用回调方法。如何从该方法调用客户端函数?

c#-4.0 - SignalR 组未获取数据

c# - 为什么在 Owin Startup 类中 auth 中间件声明的顺序很重要?

c# - 限制匹配的长度

c# - 来自 textBox 的值没有被插入到数据库中

c# - 为非常简单的类调整和简化 DataContract 序列化?

c# - 从 Web API 返回图像列表