I know there are lots of examples out there to do with SignalR but I can't seem to get it working, I was hoping that one of you may be able to show (in full) how a WebPage (threaded loop so we can see it happening over and over) could call a JS method on a Page and change a text label or create a popup or, just something so that we an see the method execute?
I'll give you my code and maybe you can point out the error, but any basic example of Server->Client invocation without a Client first making a request would be amazing!
Hub:
[HubName("chat")]
public class Chat : Hub
{
public void Send(string message)
{
// Call the addMessage method on all clients?
Clients.addMessage(message);
}
}
Calling (Threaded) method:
private void DoIt()
{
int i = 0;
while (true)
{
var hubContext = GlobalHost.ConnectionManager.GetHubContext<Chat>();
hubContext.Clients.addMessage("Doing it... " + i);
i++;
Thread.Sleep(500);
}
}
JS:
$(function () {
// Proxy created on the fly
var chat = $.connection.chat;
// Declare a function on the chat hub so the server can invoke it
chat.addMessage = function (message) {
confirm("Are you having fun?");
confirm(message);
};
// Start the connection
$.connection.hub.start();
});