Created
April 19, 2015 14:33
-
-
Save a-h/3660a6729c3450b38731 to your computer and use it in GitHub Desktop.
Caching Data in a SignalR Hub and Limiting Publisher Rights by Using a Password
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
namespace SparkLines.Hubs | |
{ | |
public class SparklinesHub : Hub | |
{ | |
static ConcurrentDictionary<string, List<int>> Values = new ConcurrentDictionary<string, List<int>>(); | |
static int maximumValues = 49; | |
public void PublishMetric(string name, int value) | |
{ | |
if (IsAuthorised()) | |
{ | |
// Update the dictionary with new data. | |
Values.AddOrUpdate(name, new List<int>() { value }, (k, v) => | |
{ | |
int skip = v.Count > maximumValues ? v.Count - maximumValues : 0; | |
return v.Skip(skip).Concat(new int[] { value }).ToList(); | |
}); | |
var valuesToSend = new List<int>() { }; | |
Values.TryGetValue(name, out valuesToSend); | |
// Publish the metrics data to connected clients. | |
Clients.All.metricsUpdated(name, valuesToSend); | |
} | |
} | |
private bool IsAuthorised() | |
{ | |
return base.Context.Headers["PublisherKey"] == "xy123123"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment