Skip to content

Instantly share code, notes, and snippets.

@jasondentler
Created February 26, 2012 01:05
Show Gist options
  • Save jasondentler/1911998 to your computer and use it in GitHub Desktop.
Save jasondentler/1911998 to your computer and use it in GitHub Desktop.
NServiceBus-and-SignalR
/// <reference path="~/scripts/lib"/>
$(function() {
var auctionHub = $.connection.auctionHub;
auctionHub.onBidAccepted = function(itemId, itemName, amount) {
$.pnotify({
pnotify_title: "Out bid",
pnotify_text: "The new high bid for " + itemName + " is " + amount + ".",
pnotify_sticker: false
});
};
$.connection.start();
});
public interface IItemService
{
string GetItemName(Guid itemId);
}
public class BidAcceptedEvent : NServiceBus.IEvent
{
public Guid ItemId { get; set; }
public Guid BidId { get; set; }
public decimal Amount { get; set; }
}
public class AuctionHub : Hub
{
}
public class AuctionHubHandler :
IHandleMessages<BidAcceptedEvent>
{
private readonly IConnectionManager _connectionManager;
private readonly IItemService _itemService;
public AuctionHubHandler(IConnectionManager connectionManager, IItemService itemService)
{
_connectionManager = connectionManager;
_itemService = itemService;
}
private dynamic Clients
{
get { return _connectionManager.GetClients<AuctionHub>(); }
}
public void Handle(BidAcceptedEvent message)
{
var itemName = _itemService.GetItemName(message.ItemId);
Clients[message.ItemId].onBidAccepted(message.ItemId, itemName, message.Amount);
}
}
public class PatientHub : Hub
{
public void AdmitPatient(Guid patientId)
{
// Admit the patient
}
}
patientHub.admitPatient(patientId);
public class StatsHub : Hub
{
}
public class SomeOtherClass
{
private readonly IConnectionManager _connectionManager;
public Something(IConnectionManager connectionManager)
{
_connectionManager = connectionManager;
}
private dynamic Clients
{
get { return _connectionManager.GetClients<StatsHub>(); }
}
public void UpdateStats(Stats stats)
{
Clients.updateStats(stats);
}
}
var statsHub = $.connection.statsHub;
statsHub.updateStats = function(stats) {
// Update the page with new stats.
}
$.connection.start();
@gabrieljoelc
Copy link

In the auction example, how does the hub context correlate between the ItemId and a specific client's connection? Also, I don't see the IConnectionManager#GetClients<THub>() methhod in SignalR 1.0. Is this from an earlier version or is there an extension method I'm missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment