Created
November 24, 2012 21:15
-
-
Save orangutanboy/4141419 to your computer and use it in GitHub Desktop.
SignalR Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNet.SignalR.Hubs; | |
using System.Timers; | |
namespace SignalRDemo | |
{ | |
public class DemoHub : Hub | |
{ | |
private readonly DemoTimer _demoTimer; | |
public DemoHub() | |
{ | |
_demoTimer = DemoTimer.Instance(); | |
} | |
public void StartUpdates() | |
{ | |
_demoTimer.StartUpdates(); | |
} | |
public void StopUpdates() | |
{ | |
_demoTimer.StopUpdates(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNet.SignalR; | |
using System.Timers; | |
namespace SignalRDemo | |
{ | |
public class DemoTimer | |
{ | |
private readonly static DemoTimer _instance = new DemoTimer(); | |
private readonly Timer _timer; | |
private int _tickCount; | |
private DemoTimer() | |
{ | |
_timer = new Timer { Enabled = false, Interval = 1000 }; | |
_timer.Elapsed += (s, e) => UpdateClients(); | |
} | |
public static DemoTimer Instance() | |
{ | |
return _instance; | |
} | |
private void UpdateClients() | |
{ | |
GlobalHost.ConnectionManager.GetHubContext<DemoHub>().Clients.All.displayCount(_tickCount++); | |
} | |
internal void StartUpdates() | |
{ | |
_timer.Enabled = true; | |
} | |
internal void StopUpdates() | |
{ | |
_timer.Enabled = false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript" src="~/Scripts/jquery-1.6.4.min.js"></script> | |
<script type="text/javascript" src="~/Scripts/jquery.signalR-1.0.0-alpha2.min.js"></script> | |
<script type="text/javascript" src="~/signalr/hubs"></script> <!-- SignalR's magic file --> | |
<title>SignalR Demo</title> | |
</head> | |
<body> | |
<div> | |
<label id="countLabel"></label> | |
<input type="button" id="startButton" value="Start" /> | |
<input type="button" id="stopButton" value="Stop" /> | |
</div> | |
<script type="text/javascript"> | |
$(document).ready(function () { | |
// register the start button's click event to call a server method | |
$('#startButton').click(function () { | |
$.connection.demoHub.server.startUpdates(); | |
}); | |
// register the stop button's click event to call a server method | |
$('#stopButton').click(function () { | |
$.connection.demoHub.server.stopUpdates(); | |
}); | |
// create a function on the client for the server to call | |
$.connection.demoHub.client.displayCount = function (count) { | |
$('#countLabel').text(count); | |
}; | |
// make the connection | |
$.connection.demoHub.connection.start(); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment