SignalR返回客户端的值只能工作一段时间

时间:2015-03-11 08:02:05

标签: c# signalr

我制作了一个“生命”通知系统,所以当我网站上的用户收到新邮件时,他们会在网站上获得通知。

它有效,但只有一些,所以我希望有人可以告诉我发生了什么以及如何解决它。

当我调试并检查totalNewMessages总是有一个0或更高的值,但从我所看到的,问题是在NotificationHub的最后一行和jquery部分之间。

如果你查看jquery代码,我添加了Alert(totalNewMessages);但它并不总是激发。可能是什么问题呢?

NotificationHub.cs

IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();


return context.Clients.Client(Users.Values.FirstOrDefault(i => i.ProfileId == UserID).ConnectionIds).RecieveNotification(totalNewMessages);

Jquery的:

<script type="text/javascript">
    $(function () {
        // Declare a proxy to reference the hub.
        var hiddenID = $('#HiddenID');
        var Check = $('#Check');
        var notifications = $.connection.notificationHub;
        // Create a function that the hub can call to broadcast messages.
        notifications.client.recieveNotification = function (totalNewMessages) {
            // Add the message to the page.
            var notification = $('#lblNotificationNumber')
            alert(totalNewMessages);
            if (totalNewMessages > 0) {
                notification.fadeIn('slow');
                notification.text(totalNewMessages);
            }
            else {
                notification.fadeOut('slow');
            }

        };
        // Start the connection.
        $.connection.hub.start().done(function () {
            if (hiddenID.val() > 0) {

                notifications.server.check(hiddenID.val());
                notifications.server.sendNotifications(hiddenID.val());


            }
        }).fail(function (e) {
            alert(e);
        });

        $('#btnLogout').click(function () {
            $.removeCookie("test");
        });

        //$.connection.hub.start();
    });
</script>

0 个答案:

没有答案