John S
John S

Reputation: 15

Signalr Calling Hub Method from Code Behind Ajax Call

I am running the stock signalr example and trying to modify the code and try something differnt. What I am wanting is on a front end button press to call an ajax call and from there call a hub method. When I do this I am not hitting my breakpoint in my hub method

Front end code change:

$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
$.ajax({

url: '/home/MyTest/',
type: "POST",
success: function () {
alert("done");
}, error: function (jqXHR, exception) {
alert("failed");
}

});
//chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});

In HomeController.cs

[HttpPost]
public void MyTest()
{
var context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
context.Clients.All.send("a","b");
}

ChatHub.cs

public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the addNewMessageToPage method to update clients.
Clients.All.addNewMessageToPage(name, message);
}
}

Upvotes: 1

Views: 982

Answers (1)

Mark C.
Mark C.

Reputation: 6460

The Hub method on the server doesn't get executed... What

var context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>(); context.Clients.All.send("a","b");

is saying is : On every client that has an open/active connection to the Hub, I'm invoking a send function on the client. Not on the Hub on the server.

Upvotes: 1

Related Questions