在新选项卡或具有SqlDependency的新浏览器SignalR中打开时,$ .connection未定义

时间:2017-05-17 13:11:51

标签: c# jquery model-view-controller signalr sqldependency

我创建了一个mvc应用程序并使用SqlDependency实现了SignalR通知功能它在单个浏览器和单个选项卡中工作,如果我在新选项卡或新浏览器中打开它通过错误$ .connection未定义。 我发现〜/ signalr / hubs只加载主页而不加载其他表格。

我的代码是: 参考Jquery

<script src="~/Scripts/jquery.signalR-2.2.2.js"></script>
<script src="~/signalr/hubs"></script>

Jquery的

$(function () {
    // Declare a proxy to reference the hub.
    var notifications = $.connection.notificationHub;

    // Create a function that the hub can call to broadcast notifications.
    notifications.client.updateNotifications = function () {
        getAllNotifications()
    };

    // Start the connection.
    $.connection.hub.start().done(function () {
        getAllNotifications();
    });
});

function getAllNotifications() {
    var tbl = $('#notification-panel');
    $.ajax({
        url: '/Group/GetNotifications',
        contentType: 'application/html ; charset:utf-8',
        type: 'GET',
        dataType: 'html',
        async: false,
        success: function (result) {
            tbl.empty().append(result);
        },
        error: function () {
        }
    });
}

通知中心

public class NotificationHub : Hub
    {
        private static string conString = ConfigurationManager.ConnectionStrings["SPContext"].ToString();

        [HubMethodName("sendNotification")]
        public static void SendNotification()
        {
            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
            context.Clients.All.updateNotifications();
        }
    }

存储库

readonly string _connString = ConfigurationManager.ConnectionStrings["SPContext"].ConnectionString;
        public IEnumerable<scNotification> GetAllNotification(string userId)
        {
            var notifications = new List<scNotification>();
            using (var connection = new SqlConnection(_connString))
            {
                connection.Open();
                //var query = _dbContext.scNotifications.Where(x => x.UserId == userId && x.IsDeleted == false) as DbQuery<scNotification>;

                using (var command = new SqlCommand(@"SELECT [Id], 
                [Title], [Description], [Url], [IsRead], [IsDeleted], [Date] FROM [dbo].[scNotification] WHERE userId = '" + userId + "'", connection))
                {
                    command.Notification = null;

                    var dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

                    if (connection.State == ConnectionState.Closed)
                        connection.Open();

                    var reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        notifications.Add(item: new scNotification
                        {
                            Id = (int)reader["Id"],
                            Title = (string)reader["Title"],
                            Description = reader["Description"] != DBNull.Value ?
                        (string)reader["Description"] : "",
                            Url = (string)reader["Url"],
                            Date = Convert.ToDateTime(reader["Date"]),
                            IsRead = (bool)reader["IsRead"],
                            IsDeleted = (bool)reader["IsDeleted"]
                        });
                    }
                }
            }
            return notifications;
        }

        private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            if (e.Type == SqlNotificationType.Change)
            {
                NotificationHub.SendNotification();
            }
        }

0 个答案:

没有答案