3

Here's an API server which can give me real time news: every minute there will be something new to retrieve. There's also my web page, with a Javascript that will ask the API to get some news once every minute.

And this is not fine... unless my web page is made for a single user and will be open only on one machine at a time (which is not the case of the internet). the API, infact, restricts the number of call I can do per minute: let's suppose the API will ban me if I do more than 1 call per minute. If 100 users will load my web page, the API will receive 100 calls per minute (!!!).

Since the flow is my web page >> calls >> the API I think there is no solution without inserting another node which lazy loads from the api server.

my web page >> calls >> my server >> calls every minute >> the API

Since the instances of my web page may be many while my server is just one I think this is the solution.

However, I have no idea if:

a) is this the correct solution? Or could I somehow get my web page to behave correctly without the need of an intermediary server?

b) how can I implement this in ASP.NET MVC4? Is there any support for server side timers?

c) even if I can get IIS to retrieve the data every minute, should I then store it in a database to serve it to my web page?

d) the api server I'm talking about is The Times Newswire API. If anyone ever used a similar API, did you really created a domain model, a database table, a service and a routine just to retrieve data from it or did you just writed some javascript code in my web page? What then if you have milions of users?

4

4 回答 4

3

您可以为此目的使用 SignalR,这是一种通过使用套接字工作的推送服务,因此可以配置为向 1,000,000 个侦听器(或更明显)发送一条消息。

去年我在创建一个小原型游戏时使用了这个效果很好,发现它非常可靠。在vs2010和vs2012中可以使用NuGet抓取包。

查看Asp.net SignalR,查看示例或简单地 google SignalR,您会找到大量示例。

于 2013-07-31T09:44:07.343 回答
1

如果我每分钟调用超过 1 次,API 将禁止我

然后,您需要一个解决方案,每分钟为您调用 API 并将其存储在您的服务器上。有很多方法可以做到这一点,具体取决于许多要求。您甚至可以编写一个静态 HTML 文件,然后将其显示给客户端。

于 2013-07-31T09:44:08.347 回答
0

您应该从服务器端调用 API。每分钟一次将其提取到您的数据库中。然后通过例如 restful 服务将其从您的数据库中提供给用户。ASP.NET MVC 4 可以做到这一切。

也是的,它确实有一个计时器。您可以查看Timer类。

您不必为使用此解决方案的 API 限制而苦恼。

于 2013-07-31T09:46:56.380 回答
0

A) 最好的解决方案可能是使用您的 Web 服务器作为 API 的中间体

B) 有很多可能性可供选择。最简单的方法是在 Web 应用程序启动时(即在 Global.asaxOnApplicationStarted事件中)启动一个新线程,并让该线程轮询外部 API 并存储结果以供您的客户端获取。如果您想完全控制此后台进程的生命周期,另一种选择是创建一个 Windows 服务并在其中托管例如您的客户端可以连接到的 WebAPI。

当然,这些只是建议。

C) 取决于您是否希望您的客户端能够访问最新的 fetch 数据,即使后台进程已失败且数据已旧。否则,您可以将其存储在某个静态字段中。只要应用程序没有终止,静态字段就会保持其值。

于 2013-07-31T09:46:58.733 回答