add hub server methods

This commit is contained in:
chsakell
2016-10-03 09:24:41 +03:00
parent b0040ac178
commit ac92ee2d30
5 changed files with 32 additions and 4 deletions

View File

@@ -13,5 +13,15 @@ namespace LiveGameFeed.Hubs
{ {
return Clients.All.messageReceived(Context.ConnectionId + "> " + message); return Clients.All.messageReceived(Context.ConnectionId + "> " + message);
} }
public Task Subscribe(int matchId)
{
return Groups.Add(Context.ConnectionId, matchId.ToString());
}
public Task Unsubscribe(int matchId)
{
return Groups.Remove(Context.ConnectionId, matchId.ToString());
}
} }
} }

View File

@@ -19,7 +19,7 @@
<span class="label label-success">{{match.Type}}</span> <span class="label label-success">{{match.Type}}</span>
</td> </td>
<td> <td>
<button type="button" class="btn btn-default btn-xs"> <button type="button" class="btn btn-default btn-xs" (click)="subscribe()">
Subscribe to feed Subscribe to feed
</button> </button>
</td> </td>

View File

@@ -1,6 +1,7 @@
import { Component, OnInit, Input } from '@angular/core'; import { Component, OnInit, Input } from '@angular/core';
import { Match } from '../interfaces'; import { Match } from '../interfaces';
import { FeedService } from '../services/feed.service';
@Component({ @Component({
selector: 'match', selector: 'match',
@@ -10,7 +11,12 @@ export class MatchComponent implements OnInit {
@Input() match: Match; @Input() match: Match;
constructor() { } constructor(private feedService: FeedService) { }
ngOnInit() { } ngOnInit() { }
subscribe() {
console.log(this.match.Id);
this.feedService.subscribeToFeed(this.match.Id);
}
} }

View File

@@ -4,7 +4,8 @@ export interface FeedSignalR extends SignalR {
} }
export interface FeedProxy { export interface FeedProxy {
client: FeedClient client: FeedClient;
server: FeedServer;
} }
export interface FeedClient { export interface FeedClient {
@@ -15,6 +16,10 @@ export interface FeedClient {
messageReceived: (message: string) => void; messageReceived: (message: string) => void;
} }
export interface FeedServer {
subscribe(matchId: number) : void;
}
export enum ConnectionState { export enum ConnectionState {
Connected = 1, Connected = 1,
Disconnected = 2, Disconnected = 2,

View File

@@ -5,7 +5,7 @@ import 'rxjs/add/operator/toPromise';
import { Observable } from "rxjs/Observable"; import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/Subject"; import { Subject } from "rxjs/Subject";
import { FeedSignalR, FeedProxy, FeedClient, ConnectionState, Match } from '../interfaces'; import { FeedSignalR, FeedProxy, FeedClient, FeedServer, ConnectionState, Match } from '../interfaces';
@Injectable() @Injectable()
export class FeedService { export class FeedService {
@@ -23,6 +23,8 @@ export class FeedService {
private updateMatchSubject = new Subject<Match>(); private updateMatchSubject = new Subject<Match>();
private messageReceivedSubject = new Subject<string>(); private messageReceivedSubject = new Subject<string>();
private server: FeedServer;
constructor(private http: Http) { constructor(private http: Http) {
this.connectionState = this.connectionStateSubject.asObservable(); this.connectionState = this.connectionStateSubject.asObservable();
this.userConnected = this.userConnectedSubject.asObservable(); this.userConnected = this.userConnectedSubject.asObservable();
@@ -37,6 +39,7 @@ export class FeedService {
// get the signalR hub named 'broadcaster' // get the signalR hub named 'broadcaster'
let connection = <FeedSignalR>$.connection; let connection = <FeedSignalR>$.connection;
let feedHub = connection.broadcaster; let feedHub = connection.broadcaster;
this.server = feedHub.server;
/** /**
* @desc callback when a new user connect to the chat * @desc callback when a new user connect to the chat
@@ -104,4 +107,8 @@ export class FeedService {
console.log(message); console.log(message);
this.messageReceivedSubject.next(message); this.messageReceivedSubject.next(message);
} }
public subscribeToFeed(matchId: number) {
this.server.subscribe(matchId);
}
} }