I have been struggling to get SignalR working despite the many examples available. I am hoping that someone can provide a clear demonstration of how a WebPage with a threaded loop can call a JavaScript method on a page to change a text label or create a popup. Essentially, I would like to see the method in action without the need for the client to make an initial request.
Here is my code, and any guidance on where I may be going wrong would be greatly appreciated. But more importantly, a basic example of server-to-client invocation without a prior request from the client would be fantastic!
Hub:
[HubName("chat")]
public class Chat : Hub
{
public void Send(string message)
{
// Call the addMessage method on all clients?
Clients.addMessage(message);
}
}
Threaded method being called:
private void DoIt()
{
int i = 0;
while (true)
{
var hubContext = GlobalHost.ConnectionManager.GetHubContext<Chat>();
hubContext.Clients.addMessage("Doing it... " + i);
i++;
Thread.Sleep(500);
}
}
JavaScript code:
$(function () {
// Proxy created dynamically
var chat = $.connection.chat;
// Define a function on the chat hub for server invocation
chat.addMessage = function (message) {
confirm("Are you having fun?");
confirm(message);
};
// Start the connection
$.connection.hub.start();
});