简体   繁体   English

使用浏览器从服务器提取消息来更新消息

[英]Updating Messages with Browser Pulling Messages from Server

I am tasked with creating a web page (think twitter) that updates when new messages are added to the database. 我的任务是创建一个网页(想想twitter),该网页在将新消息添加到数据库时会更新。 When a message is removed from the database, it also must be removed from the client. 从数据库中删除消息时,还必须从客户端中删除消息。 It is possible that multiple clients can be accessing the same messages at the same time. 多个客户端可能同时访问同一消息。 Other actions can occur, such as a stop command issued on the server. 可能会发生其他动作,例如在服务器上发出停止命令。 Once this happens all the messages on the server will stop showing. 一旦发生这种情况,服务器上的所有消息将停止显示。

What I am looking for is an architecture for solving this problem. 我正在寻找一种解决此问题的体系结构。

Technologies that I am using are .Net 4.5, ASP.Net MVC and KnockoutJs. 我使用的技术是.Net 4.5,ASP.Net MVC和KnockoutJs。 Nodejs could be used, but I'd need to know the benefit of using nodejs over using SignalR. 可以使用Node.js,但是我需要知道使用Node.js而不是使用SignalR的好处。

My currently implementation is using a javascript timer which is polling the server every 30 seconds for new messages. 我目前的实现方式是使用JavaScript计时器,该计时器每30秒轮询服务器一次以获取新消息。 It works, but the polling feels dirty. 它可以工作,但是轮询感觉很脏。

Can't comment on ASP.NET - but have used Node.js together with Knockout for this. 无法在ASP.NET上发表评论-但为此使用了Node.js和Knockout。 I have used both WebSockets (via socket.io library) and also Server Sent Events (SSE) to push updates to the client model. 我既使用了WebSockets(通过socket.io库),也使用了服务器发送事件(SSE)将更新推送到客户端模型。

Sounds like SSE would be a good fit in this case. 听起来SSE很适合这种情况。 The key is whatever your database technology is should support emitting changes events to your node middle-ware so that you can send this to the browser. 关键在于,无论您的数据库技术是什么,都应支持向节点中间件发出更改事件,以便您可以将其发送到浏览器。

After more research, the polling method is the optimal solution for the technologies in involved. 经过更多的研究,轮询方法是所涉及技术的最佳解决方案。

The crux of the problem is there is no notification of a new message, which would prompt a change in the system. 问题的症结在于没有新消息的通知,这会提示系统发生更改。 Currently, a new message is received when it is committed to the database. 当前,将新消息提交到数据库时会收到该消息。 SQL Server does not have a notification mechanism (this is not 100 percent true, but it not a dependency I wish to take on). SQL Server没有通知机制(这不是100%正确,但不是我希望承担的依赖项)。 In long the run, the optimal system would be to implement a publisher/subscribe motel using SignalR or nodejs which would deliver real-time messages to the client. 从长远来看,最佳系统将是使用SignalR或nodejs实现发布者/订阅者汽车旅馆,该公司将向客户端传递实时消息。 For this to happen it would require a complete re-architecture of the application. 为此,将需要对应用程序进行完整的重新架构。

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

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