简体   繁体   English

通过长轮询检查数据库是否有变化

[英]Check database for changes via long polling

Im creating a chat app in ASP.NET MVC3. 我在ASP.NET MVC3中创建了一个聊天应用程序。 im using long polling and AsyncController to do so 即时通讯使用长轮询和AsyncController这样做

when a user posts a chat its saved in database , to retrieve should i constantly check database for change in record or after definite interval or is there an better/ efficient way of doing it 当用户发布保存在数据库中的聊天时,要检索我是否应该不断检查数据库中的记录更改或确定的间隔后,还是有更好/更有效的方法

i came across this question but could not get a usable answer. 我遇到了这个问题,但无法得到一个有用的答案。

You may take a look at SignalR for an efficient way. 您可以以有效的方式查看SignalR Contrary to the standard polling mechanism (in which you are sending requests at regular intervals to check for changes), SignalR uses a push mechanism in which the server sends notifications to connected clients to notify them about changes. 与标准轮询机制(您定期发送请求以检查更改)相反,SignalR使用推送机制,服务器向连接的客户端发送通知,以通知他们有关更改的信息。

Since you're already using long polling and an asynccontrolller, why not create a message pool? 由于您已经在使用长轮询和异步控制器,为什么不创建消息池? Take a look at this solution . 看看这个解决方案

In a nutshell, instead of just writing the updated chat to the database, you should also stick it in some sort of queue. 简而言之,您应该将其添加到某种队列中,而不是仅仅将更新的聊天写入数据库。 Then each user's async thread is listening to that pool waiting for a message to appear. 然后每个用户的异步线程正在侦听该池,等待消息出现。 When one appears return the data to the user through your normal operation. 当出现一个时,通过正常操作将数据返回给用户。 When all listening threads have picked up the message it can be removed from the queue. 当所有侦听线程都拾取了消息时,可以从队列中删除它。 This will prevent you from having several threads hammering your database looking for a new message. 这将阻止您使用多个线程来查找数据库以查找新消息。

You can give PServiceBus(http://pservicebus.codeplex.com/) a try and here is a sample web chat app(http://74.208.226.12/ChatApp/chat.html) running and does not need database in between to pass message between two web clients. 您可以尝试PServiceBus(http://pservicebus.codeplex.com/),这是一个示例网络聊天应用程序(http://74.208.226.12/ChatApp/chat.html)运行,并且之间不需要数据库在两个Web客户端之间传递消息。 If you want to persist data in the database for logging sake, you can always subscribe to the chat message and log it to database. 如果要将数据保留在数据库中以便记录,则可以始终订阅聊天消息并将其记录到数据库。

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

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