SignalR does not call jQuery function

I have this piece of code on my page, but it does not start when I make changes to the database, which can be a problem. It starts well when I load the page, this function performs twice, but if I send a message to the database, it will not be executed.

 

$(function () {

var chat = $.connection.chatHub;
chat.client.allTalks = function () {
    refresh();
};
$.connection.hub.start();
    refresh();

});

SERVER (HUB):

    [HubName("chatHub")]
    public class ChatHub : Hub
    {
        public static void AllTalks()
        {
            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
            context.Clients.All.allTalks();
        }
    }

Handler

...

using (SqlCommand command = new 
SqlCommand(@"SELECT * FROM [dbo].[chat_talks]", connection)) {

// CONTENT

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

...   }

        public void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            ChatHub.AllTalks();
        }

global.asax

        protected void Application_Start(object sender, EventArgs e)
        {
            SqlDependency.Start(ConfigurationManager.ConnectionStrings["ProjectSellerConnection"].ConnectionString);
        }

enter image description here

+4
source share
1 answer

First, it is redundant to have the first line in your server code. There is no need to call hubContext inside the hub. You can simply:

public static void AllTalks()
{
    Clients.All.allTalks();
}

, , , SQL Dependency. SignalR ( , ):

var hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
hubContext.Clients.All.allTalks();

, , MVC WebAPI, , - , , . , , SQL Dependency, , , , , - , , SignalR.

, , , , , .

+6

Source: https://habr.com/ru/post/1531790/


All Articles