G.Thirunavukkarasu G.Thirunavukkarasu - 8 days ago 7
C# Question

How do i Add callBack Functionality to my hubClass in signalR for update the current work status to client with time delay?

I am new to signalR.I am developing a simple application in signalR. The Requirement is ,the signalR need to be update the current time to client repeatedly with some time delay.While i am starts running, the code i get server response for a time only.it's not repeated again and again. i need to callback the sameHubclass for update the current time to client again and again!

here is my Code:-

Javascript Client Code:-

<body>
<div class="container">
<input type="hidden" id="name" />
</div>
<script src="Scripts/jquery-1.6.4.min.js"></script>
<script src="Scripts/jquery.signalR-2.2.1.min.js"></script>
<script src="signalr/hubs"></script>
<script type="text/javascript">
$(function () {
var chat = $.connection.timeHub;
chat.client.broadcastMessage = function (name,current) {
var now = current;
console.log(current);
$('div.container').append('<p><strong>'+name+"="
+ now + '</strong></p>');

};
$("#name").val(prompt("Enter the UserName", ""));

$.connection.hub.start().done(function () {
chat.server.send($("#name").val());

});

});
</script>
</body>


HubClass:-

public class timeHub : Hub
{
public void send(string name)
{

while(true)
{

string current = DateTime.Now.ToString("HH:mm:ss:tt");
Clients.All.broadcastMessage(name,current);
Console.WriteLine(current);
Console.ReadLine();
System.Threading.Thread.Sleep(3000);
send(name);
}
}
}


MyOwin Startup Class:-

[assembly: OwinStartup(typeof(Time.Startup))]

namespace Time
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}


How do i add callback functionality for my HubClass in Javascript Client Code in SignalR.

here is my current Output without callback function and timedelay:-

It update the current Client in  time only

but the signalR and .net client application or working fine.I need to time delay for hubclass and callback the samehub class periodically?

Here another output

Is it possible to callback the same hub class in server with some time delay?
For that i am added, time delay in hubclass. and i callback the hubclass in server .It won't work.Could anyone provide me an solution for this?

Answer

Ideally you would want to use a timer that calls the broadcastMessage method with the current time. There are some issues with your send hub method though. Also SignalR instantiates the hub class per each call and state is not maintained.

Comments