这是我第一次玩SignalR.我想建立一个通知系统,其中定期服务器检查,看看是否有什么(查询数据库)来播放,如果有那么它广播到所有的客户.我碰到这个职位#2,想知道是否修改代码以使DB调用在一个特定的时间间隔确实是这样做的正确方法.如果不是,有更好的方法吗?
我确实在这里发布了许多与通知相关的问题,但没有任何代码.因此这篇文章.
这是我正在使用的确切代码:
public class NotificationHub : Hub
{
public void Start()
{
Thread thread = new Thread(Notify);
thread.Start();
}
public void Notify()
{
List<CDCNotification> notifications = new List<CDCNotification>();
while (true)
{
notifications.Clear();
notifications.Add(new CDCNotification()
{
Server = "Server A", Application = "Some App",
Message = "This is a long ass message and amesaadfasd asdf message",
ImgURL = "../Content/Images/accept-icon.png"
});
Clients.shownotification(notifications);
Thread.Sleep(20000);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已经看到一些奇怪的行为,其中通知比他们应该更频繁.即使我应该每隔20秒就能得到它,我会在4-5秒左右得到它并且我收到多条消息.这是我的客户:
var notifier = $.connection.notificationHub;
notifier.shownotification = function (data) {
$.each(data, function (i, sample) {
var output = Mustache.render("<img class='pull-left' src='{{ImgURL}}'/> <div><strong>{{Application}}</strong></div><em>{{Server}}</em> <p>{{Message}}</p>", sample);
$.sticky(output);
});
};
$.connection.hub.start(function () { notifier.start(); });
Run Code Online (Sandbox Code Playgroud)
几个笔记:
为了解决这个问题,你需要这样的东西(再次,Web应用程序中的线程通常不是一个好主意):
public class NotificationHub : Hub
{
public static bool initialized = false;
public static object initLock = new object();
public void Start()
{
if(initialized)
return;
lock(initLock)
{
if(initialized)
return;
Thread thread = new Thread(Notify);
thread.Start();
initialized = true;
}
}
public void Notify()
{
List<CDCNotification> notifications = new List<CDCNotification>();
while (true)
{
notifications.Clear();
notifications.Add(new CDCNotification() { Server = "Server A", Application = "Some App", Message = "This is a long ass message and amesaadfasd asdf message", ImgURL = "../Content/Images/accept-icon.png" });
Clients.shownotification(notifications);
Thread.Sleep(20000);
}
}
}
Run Code Online (Sandbox Code Playgroud)
的静态初始化的标志可防止多个线程被创建.围绕它的锁定是为了确保标志仅设置一次.
| 归档时间: |
|
| 查看次数: |
3721 次 |
| 最近记录: |