public
Last active

Consensus: SignalR + TypeScript found here http://coderjournal.com/2013/07/signalr-and-typescript/

  • Download Gist
Poker.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
namespace Consensus.Hubs
{
public class Poker : Hub
{
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
 
private static Dictionary<string, PokerUser> _users = new Dictionary<string, PokerUser>();
private static List<PokerRoom> _rooms = new List<PokerRoom>();
 
public PokerUser Join(PokerUser user)
{
_logger.Info("{0} joined consensus", user.Email);
 
var userRef = _users.Where(x => x.Value.Email == user.Email).Select(x => new {
ConnectionId = x.Key,
User = x.Value
}).FirstOrDefault();
 
if (userRef != null && userRef.ConnectionId != Context.ConnectionId)
_users.Remove(userRef.ConnectionId);
 
user = _users.Where(x => x.Key == Context.ConnectionId).Select(x => x.Value).FirstOrDefault() ?? user;
 
if (!_users.ContainsKey(Context.ConnectionId))
_users.Add(Context.ConnectionId, user);
 
return user;
}
 
public PokerRoom JoinRoom(PokerRoom room)
{
var user = _users.Where(x => x.Key == Context.ConnectionId).Select(x => x.Value).FirstOrDefault();
 
if (user == null)
throw new Exception("No user with this connection Id has joined yet.");
 
_logger.Info("{0} joined {1} room", user.Email, room.Name);
 
room = _rooms.FirstOrDefault(x => x.Name == room.Name) ?? room;
 
if (!_rooms.Contains(room))
_rooms.Add(room);
 
if (room.Users.All(x => x.Email != user.Email)) {
room.Users.Add(user);
}
 
// tell the people in this room that you've joined
Clients.Group(room.Name).userChanged(user);
 
Groups.Add(Context.ConnectionId, room.Name);
 
return room;
}
 
private void AssertContextUserJoinedRoom(string roomName)
{
var user = _users.Where(x => x.Key == Context.ConnectionId).Select(x => x.Value).FirstOrDefault();
 
if (user == null)
throw new Exception("No user with this connection Id has joined yet.");
 
var room = _rooms.FirstOrDefault(x => x.Name == roomName);
 
if (room == null)
throw new Exception("No room with this name exists.");
 
if (room.Users.All(x => x.Email != user.Email)) {
throw new Exception("User hasn't joined this room yet.");
}
}
 
public void LeaveRoom(PokerRoom room, PokerUser user)
{
AssertContextUserJoinedRoom(room.Name);
room = _rooms.FirstOrDefault(x => x.Name == room.Name);
 
if (room.Users.All(x => x.Email != user.Email))
throw new Exception("User being removed hasn't joined this room yet.");
 
room.Users = room.Users.Where(x => x.Email != user.Email).ToList();
room.Cards = room.Cards.Where(x => x.User.Email != user.Email).ToList();
 
// tell the people in this room that user has been removed
Clients.Group(room.Name).userRemoved(user);
}
 
public void ResetRoom(PokerRoom room)
{
AssertContextUserJoinedRoom(room.Name);
 
room = _rooms.FirstOrDefault(x => x.Name == room.Name);
room.Topic = "";
room.Cards = new List<PokerCard>();
 
// tell the people in this room that the topic has changed
Clients.Group(room.Name).resetRoom(room);
}
 
public void ShowAllCards(PokerRoom room, bool show)
{
AssertContextUserJoinedRoom(room.Name);
 
// tell the people in this room that the topic has changed
Clients.Group(room.Name).showAllCards(show);
}
 
public void ChangeRoomTopic(PokerRoom room, string topic)
{
AssertContextUserJoinedRoom(room.Name);
 
room = _rooms.FirstOrDefault(x => x.Name == room.Name);
room.Topic = topic;
 
// tell the people in this room that the topic has changed
Clients.Group(room.Name).roomTopicChanged(topic);
}
 
public void ChangedCard(PokerRoom room, string cardValue)
{
AssertContextUserJoinedRoom(room.Name);
 
var user = _users.Where(x => x.Key == Context.ConnectionId).Select(x => x.Value).FirstOrDefault();
room = _rooms.FirstOrDefault(x => x.Name == room.Name);
 
var card = room.Cards.FirstOrDefault(x => x.User.Email == user.Email);
 
if (card == null) {
card = new PokerCard {
User = user,
Value = cardValue
};
room.Cards.Add(card);
}
 
card.Value = cardValue;
 
// tell the people in this room that your card has changed
Clients.Group(room.Name).cardChanged(card);
}
 
public override Task OnDisconnected()
{
var user = _users.Where(x => x.Key == Context.ConnectionId).Select(x => x.Value).FirstOrDefault();
 
if (user != null) {
user.Disconnected = DateTimeOffset.UtcNow;
 
// tell the people in this room that you've been disconnected
foreach(var room in _rooms.Where(x => x.Users.Contains(user)))
Clients.Group(room.Name).userChanged(user);
}
 
return base.OnDisconnected();
}
}
}
PokerCard.cs
C#
1 2 3 4 5 6 7 8
namespace Consensus.Models
{
public class PokerCard
{
public PokerUser User { get; set; }
public string Value { get; set; }
}
}
PokerRoom.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
namespace Consensus.Models
{
public class PokerRoom
{
public PokerRoom()
{
Users = new List<PokerUser>();
Cards = new List<PokerCard>();
}
 
public string Name { get; set; }
public string Topic { get; set; }
 
public virtual ICollection<PokerUser> Users { get; set; }
public virtual ICollection<PokerCard> Cards { get; set; }
}
}
PokerUser.cs
C#
1 2 3 4 5 6 7 8 9 10
namespace Consensus.Models
{
public class PokerUser
{
public string Name { get; set; }
public string Email { get; set; }
 
public DateTimeOffset? Disconnected { get; set; }
}
}
consensus.pokerRoom.ts
TypeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/// <reference path="typings/jquery/jquery.d.ts" />
/// <reference path="typings/signalr/signalr.d.ts" />
interface IPokerRoomClient {
userChanged(user: Consensus.PokerUser);
userRemoved(user: Consensus.PokerUser);
resetRoom(room: Consensus.PokerRoom);
showAllCards(show: boolean);
roomTopicChanged(topic: string);
cardChanged(card: Consensus.PokerCard);
}
interface IPokerRoomServer {
join(user: Consensus.PokerUser): JQueryPromise;
joinRoom(room: Consensus.PokerRoom): JQueryPromise;
leaveRoom(room: Consensus.PokerRoom, user: Consensus.PokerUser): JQueryPromise;
resetRoom(room: Consensus.PokerRoom): JQueryPromise;
showAllCards(room: Consensus.PokerRoom, show: boolean): JQueryPromise;
changeRoomTopic(room: Consensus.PokerRoom, topic: string): JQueryPromise;
changedCard(room: Consensus.PokerRoom, value: string): JQueryPromise;
}
interface HubProxy {
client: IPokerRoomClient;
server: IPokerRoomServer;
}
interface SignalR {
poker: HubProxy;
}
module Consensus {
export class PokerUser {
public Name: string;
public Email: string;
public Disconnected: string;
}
export class PokerRoom {
public Name: string;
public Topic: string;
public Users: PokerUser[];
public Cards: PokerCard[];
}
export class PokerCard {
public User: PokerUser;
public Value: string;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.