Created
February 26, 2012 01:05
-
-
Save jasondentler/1911998 to your computer and use it in GitHub Desktop.
NServiceBus-and-SignalR
This file contains hidden or 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
/// <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(); | |
}); |
This file contains hidden or 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
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); | |
} | |
} |
This file contains hidden or 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
public class PatientHub : Hub | |
{ | |
public void AdmitPatient(Guid patientId) | |
{ | |
// Admit the patient | |
} | |
} |
This file contains hidden or 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
patientHub.admitPatient(patientId); |
This file contains hidden or 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
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); | |
} | |
} |
This file contains hidden or 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
var statsHub = $.connection.statsHub; | |
statsHub.updateStats = function(stats) { | |
// Update the page with new stats. | |
} | |
$.connection.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the auction example, how does the hub context correlate between the
ItemId
and a specific client's connection? Also, I don't see theIConnectionManager#GetClients<THub>()
methhod in SignalR 1.0. Is this from an earlier version or is there an extension method I'm missing?