From 69afcd0108170642b9f445d2097b2d4ad4f44d4b Mon Sep 17 00:00:00 2001 From: chsakell Date: Wed, 5 Oct 2016 12:54:45 +0300 Subject: [PATCH] set connection id on start --- Hubs/Broadcaster.cs | 4 +++- Views/Home/Index.cshtml | 3 +-- app/home/home.component.html | 2 +- app/home/home.component.ts | 17 +++++++++++++++-- app/shared/components/chat.component.html | 5 +++++ app/shared/components/chat.component.ts | 1 + app/shared/interfaces.ts | 2 +- app/shared/services/feed.service.ts | 14 +++++++------- wwwroot/css/site.css | 2 +- 9 files changed, 35 insertions(+), 15 deletions(-) diff --git a/Hubs/Broadcaster.cs b/Hubs/Broadcaster.cs index feb51f1..8ab6585 100644 --- a/Hubs/Broadcaster.cs +++ b/Hubs/Broadcaster.cs @@ -7,9 +7,11 @@ namespace LiveGameFeed.Hubs { public override Task OnConnected() { - return Clients.All.userConnected("New connection " + Context.ConnectionId); + // Set connection id for just connected client only + return Clients.Client(Context.ConnectionId).setConnectionId(Context.ConnectionId); } + // Server side methods called from client public Task Subscribe(int matchId) { return Groups.Add(Context.ConnectionId, matchId.ToString()); diff --git a/Views/Home/Index.cshtml b/Views/Home/Index.cshtml index cef4737..4bbd67a 100644 --- a/Views/Home/Index.cshtml +++ b/Views/Home/Index.cshtml @@ -37,8 +37,7 @@
- +
\ No newline at end of file diff --git a/app/home/home.component.ts b/app/home/home.component.ts index a1f0d5a..1f044e3 100644 --- a/app/home/home.component.ts +++ b/app/home/home.component.ts @@ -12,6 +12,7 @@ import { SignalRConnectionStatus } from '../shared/interfaces'; export class HomeComponent implements OnInit { matches: Match[]; + connectionId: string; error: any; constructor(private dataService: DataService, @@ -20,6 +21,8 @@ export class HomeComponent implements OnInit { ngOnInit() { let self = this; + self.listenForConnection(); + self.feedService.connectionState .subscribe( connectionState => { @@ -60,7 +63,6 @@ export class HomeComponent implements OnInit { for (var i = 0; i < self.matches.length; i++) { if (self.matches[i].Id === feed.MatchId) { if (!self.matches[i].Feeds) { - console.log('initializing for match ' + self.matches[i].Id); self.matches[i].Feeds = new Array(); } self.matches[i].Feeds.unshift(feed); @@ -74,8 +76,19 @@ export class HomeComponent implements OnInit { }); } + listenForConnection() { + let self = this; + // Listen for connected / disconnected events + self.feedService.setConnectionId.subscribe( + id => { + console.log(id); + self.connectionId = id; + } + ); + } + updateSubscription(subscription: any) { - if(subscription.subscribe === true) + if (subscription.subscribe === true) this.feedService.subscribeToFeed(subscription.matchId); else this.feedService.unsubscribeFromFeed(subscription.matchId); diff --git a/app/shared/components/chat.component.html b/app/shared/components/chat.component.html index 629f661..be56c2f 100644 --- a/app/shared/components/chat.component.html +++ b/app/shared/components/chat.component.html @@ -10,6 +10,11 @@ {{match.Id}} {{match.Host}} vs {{match.Guest}} + + + {{connection}} + +
diff --git a/app/shared/components/chat.component.ts b/app/shared/components/chat.component.ts index f44dc4e..24d6f15 100644 --- a/app/shared/components/chat.component.ts +++ b/app/shared/components/chat.component.ts @@ -10,6 +10,7 @@ import { FeedService } from '../services/feed.service'; export class ChatComponent implements OnInit { @Input() matches: Match[]; + @Input() connection: string; messages: ChatMessage[]; constructor(private feedService: FeedService) { } diff --git a/app/shared/interfaces.ts b/app/shared/interfaces.ts index f87d129..1f0182d 100644 --- a/app/shared/interfaces.ts +++ b/app/shared/interfaces.ts @@ -9,7 +9,7 @@ export interface FeedProxy { } export interface FeedClient { - userConnected: (user: any) => void; + setConnectionId: (id: string) => void; userDisconnected: (id: string) => void; updateMatch: (match: Match) => void; diff --git a/app/shared/services/feed.service.ts b/app/shared/services/feed.service.ts index ef94bd6..580da60 100644 --- a/app/shared/services/feed.service.ts +++ b/app/shared/services/feed.service.ts @@ -13,14 +13,14 @@ export class FeedService { currentState = SignalRConnectionStatus.Disconnected; connectionState: Observable; - userConnected: Observable; + setConnectionId: Observable; updateMatch: Observable; addFeed: Observable; addChatMessage: Observable; private connectionStateSubject = new Subject(); - private userConnectedSubject = new Subject(); - + + private setConnectionIdSubject = new Subject(); private updateMatchSubject = new Subject(); private addFeedSubject = new Subject(); private addChatMessageSubject = new Subject(); @@ -30,7 +30,7 @@ export class FeedService { constructor(private http: Http) { this.connectionState = this.connectionStateSubject.asObservable(); - this.userConnected = this.userConnectedSubject.asObservable(); + this.setConnectionId = this.setConnectionIdSubject.asObservable(); this.updateMatch = this.updateMatchSubject.asObservable(); this.addFeed = this.addFeedSubject.asObservable(); this.addChatMessage = this.addChatMessageSubject.asObservable(); @@ -48,7 +48,7 @@ export class FeedService { * @desc callback when a new user connect to the chat * @param User user, the connected user */ - feedHub.client.userConnected = user => this.onUserConnected(user); + feedHub.client.setConnectionId = id => this.onSetConnectionId(id); /** * @desc callback when match score is updated @@ -77,8 +77,8 @@ export class FeedService { } // Client side methods - private onUserConnected(user: any) { - this.userConnectedSubject.next(user); + private onSetConnectionId(id: string) { + this.setConnectionIdSubject.next(id); } private onUpdateMatch(match: Match) { diff --git a/wwwroot/css/site.css b/wwwroot/css/site.css index 364ecbd..87b0ab2 100644 --- a/wwwroot/css/site.css +++ b/wwwroot/css/site.css @@ -33,7 +33,7 @@ th, td { } .chat-table { - height: 240px; + height: 253px; overflow: auto; }