问题描述
我有一个asp.net MVC 4.0应用程序。在我的观点之一,我希望显示的当前新闻列表(的IEnumerable<新闻>
)。从Web API来
I have an asp.net MVC 4.0 application. In one of my views, I wish to display a list of current news (IEnumerable<News>
) coming from a Web API.
的事情是,新闻的列表可以或可以不被经常更新。换言之,平均每天,后端团队可以具有使用后端CMS进入从5到45的消息。
The thing is that the list of news may or may not be updated frequently. In other words, on an average day, the backend team may have to enter from 5 to 45 news using the backend CMS.
我需要找出一种方法来刷新消息的用户列表中的他的浏览器中没有的,当然,刷新整个网页。
I need to figure out a way to refresh the user’s list of news within his browser without, of course, refreshing the entire web page.
我最初的想法本来是要加入一段JavaScript 的setInterval()
方法,将调用Web API从而每隔X毫秒拉动信息和刷新的新闻列表。
My initial thought would have been to add a JavaScript setInterval()
method that would call the Web API every X milliseconds thus pulling the information and refreshing the list of news.
虽然这会工作,我想用不同的方法,并使用 SignalR
,因为我相信,要求什么 SignalR $适合C $ C>所提供的。
Although this would work, I would like to use a different approach and use SignalR
since I believe the requirement fits with what SignalR
has to offer.
阅读和 SignalR
看一些例子之后,我开始明白它背后的想法,但我还是有点糊涂了。
After reading and looking at some examples on SignalR
, I’m starting to understand the idea behind it but I’m still a bit confused.
我看到的是聊天室和游戏,用户可以点击的东西触发的枢纽通话例子
。
The examples I see are Chat rooms and games where users can click stuff which triggers a call to the hub
.
在我的任务,用户的将不会有什么点击。在中心
(服务器)应该负责调用客户端每隔X毫秒更新的新闻列表。
In my task, the user WILL NOT have anything to click. The hub
(server) should be responsible of calling the client every X milliseconds to update the list of news.
在性能方面,它是荒谬的在每个X毫秒发送消息的整个列表,并覆盖用户界面。
In terms of performance, it would be ridiculous to send the entire list of news at each X milliseconds and overwrite the UI.
我需要一个方法来比较的消息当前显示在用户的与轮毂内的消息的新的列表的浏览器的列表。一旦比较的话,只能发新闻的新的比较列表。
I would need a way to compare the list of news currently showing in the user’s browser with the new list of news inside the hub. Once the compare is done, only send the new compared list of news.
有没有人有这样一个场景中的任何例子或者可以给我如何得到这个开始有些指针?
Does anyone have any example of such a scenario or can give me some pointers on how to get this started?
感谢
推荐答案
我已经做了一个名为lib目录 SignalR.EventAggregatorProxy
I have made a lib called SignalR.EventAggregatorProxy
它提供了后端服务总线或事件聚合和无缝转发所有或选定的事件到客户端之间的代理。客户端订阅的某些事件,但你也可以创建自定义的限制,使用户只接收一个选择上述事故。
It serves has a proxy between a backend service bus or event aggregator and seamlessly forwards all or selected events to the client. The client subscribes to certain events but you can also create custom constraints so that the user only receives a selection of said event.
检查如何设置了维基
安装使用的NuGet
Install using nuget
Install-Package SignalR.EventAggregatorProxy
一旦配置好火,当新闻文章添加或更新一个事件,你可以启动它在你的后台,域名,DAL任何地方等,只要你的服务总线可以把它捡起来我的代理将它捡起来了。
Once configured fire a event when a news article is added or updated, you can fire it anywhere in your backend, domain, DAL, etc, as long as your service bus can pick it up my proxy will pick it up too.
或为UI使用淘汰赛角度来创建动态列表,可以推动更改UI。装载初始数据第一次用户连接和一个监听器添加到该事件。一旦事件进来检查,如果该项目是在列表中,如果是更新它,如果它不添加它。
For the UI use Knockout or Angular to create a dynamic list that can push changes to the UI . Load the initial data first time the user connects and add a listener to the event. Once a event come in check if the item is in the list, if it is update it, if its not add it.
的SqlDependency
林书豪顾名思义就是罚款微不足道的系统,但是一旦你的系统,你的增长不希望有那样直接耦合到SQL的
SqlDependency
that Lin suggests is fine for trivial systems, but once your system grows you do not want to have that kind of coupling directly to SQL
这篇关于使用SignalR新闻清爽名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!