【发布时间】:2011-08-12 11:16:19
【问题描述】:
我有一个需要在 Web 客户端 (ASP.NET MVC) 上进行实时更新的需求。我可以扭转它的唯一方法是实现 COMET 技术(ServerPush/Reverse-AJAX)技术。
场景是: 用户A在不同的网站客户端保存消息。然后,用户 B 将自动获取 用户“A” 在不同浏览器中所做的更新。
我实际上完成了这个架构的解决方案: ASP.NET MVC - 在 WCF 上执行 jquery ajax(发布)请求(长池)。 WCF - 每隔 1 秒对数据库(SQL Server)进行轮询。如果新数据已添加到数据库中,轮询将中断,并在客户端返回数据。
WCF COMET 方法伪代码:
private Message[] GetMessages(System.Guid userID)
{
var messages = new List<Message>();
var found = false;
/* declare connection, command */
while (!found )
{
try
{
/* open connection - connection.Open(); */
/* do some database access here */
/* close connection - connection.Close(); */
/* if returned record > 0 then process the Message and save to messages variable */
/* sleep thread : System.Threading.Thread.Sleep(1000); */
found = true;
}
finally
{
/* for sure, incase of exception */
/* close connection - connection.Close(); */
}
}
return messages.ToArray();
}
我的担忧和问题是:这是在 WCF 中执行轮询技术的最佳方法(间隔 1 秒)吗?
原因:我最大限度地利用了数据库连接池,我希望这种技术不会出现问题。
注意:这是一个使用 WCF 给定属性的多线程实现。
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall), ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = true)]
【问题讨论】: