简体   繁体   English

使用ASP.NET CORE和Angular2进行实时更新

[英]Real time updates with ASP.NET CORE and Angular2

We are working on a project in ASP.NET CORE (server side) and Angular2 (client side). 我们正在使用ASP.NET CORE(服务器端)和Angular2(客户端)中的项目。

We would like to achieve the functionality of real time updates. 我们希望实现实时更新的功能。 Eg if someone updates some values at server side, these changes are reflected on client side without the user having to refresh the page (the same functionality that eg Meteor establishes out of the box). 例如,如果有人在服务器端更新某些值,则这些更改将反映在客户端上,而无需用户刷新页面(例如,Meteor即开即用地建立的相同功能)。

I thought that RXJS could do this job, however I can achieve this functionality with RXJS, I have to poll the server for changes. 我以为RXJS可以完成这项工作,但是我可以通过RXJS实现此功能,我必须轮询服务器以查找更改。 Since this will cause unneeded load at the server (even when caching is enabled) I don't really like this option. 因为这将导致服务器上不必要的负载(即使启用了缓存),所以我不太喜欢此选项。

So, I think there are better ways to achieve this functionality but I don't know which technologies to combine. 因此,我认为有更好的方法来实现此功能,但我不知道要结合哪些技术。 For ASP.NET CORE, SignalR is not yet available so this is no option. 对于ASP.NET CORE,SignalR尚不可用,因此这是没有选择的。 Do I have to look into WebSockets, or what am I missing? 我需要研究WebSockets,还是我缺少什么?

If you don't require a bi-directional data binding between the server and clients and you are only interested in getting updates (I assume you will be changing the information using any RESTful API), SSE technology should work the best for you. 如果您不需要服务器和客户端之间的双向数据绑定,并且只对获取更新感兴趣(我想您将使用任何RESTful API更改信息),那么SSE技术应该对您最有效。

For client side implementation: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events 对于客户端实施: https : //developer.mozilla.org/zh-CN/docs/Web/API/Server-sent_events/Using_server-sent_events

For server side implementation using ASPNET Core check this great answer: ASPNET Core Server Sent Events / Response flush 对于使用ASPNET Core的服务器端实现,请检查以下最佳答案: ASPNET Core Server发送事件/响应刷新

If you don't want to poll the server then you must use a permanent connection to the server to allow it to send updates to the client. 如果您不想轮询服务器,则必须使用与服务器的永久连接,以允许它向客户端发送更新。

The best fitted technology for this task is a WebSocket, but you can use a page which doesn't finish and go flushing() data to the response. 最适合此任务的技术是WebSocket,但是您可以使用未完成的页面并将flushing()数据传递给响应。

For the second type, this will give you an idea on how to go, just use an ajax request and whenever you receive some data process it. 对于第二种类型, 将使您了解如何进行操作,只需使用ajax请求,并且每当收到一些数据处理该请求时,就可以使用它。

But again, better use WebSockets as they're designed for this type of task. 但是同样,最好使用WebSocket,因为它们是为此类任务而设计的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM