diff --git a/Controllers/MessagesController.cs b/Controllers/MessagesController.cs new file mode 100644 index 0000000..6b14d5c --- /dev/null +++ b/Controllers/MessagesController.cs @@ -0,0 +1,25 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR.Infrastructure; +using LiveGameFeed.Hubs; +using LiveGameFeed.Models; + +namespace LiveGameFeed.Controllers +{ + [Route("api/[controller]")] + public class MessagesController : ApiHubController + { + public MessagesController( + IConnectionManager signalRConnectionManager) + : base(signalRConnectionManager) + { + + } + + // POST api/messages + [HttpPost] + public void Post([FromBody]ChatMessage message) + { + this.Clients.Group(message.MatchId.ToString()).addChatMessage(message); + } + } +} diff --git a/Models/ChatMessage.cs b/Models/ChatMessage.cs new file mode 100644 index 0000000..95a28a3 --- /dev/null +++ b/Models/ChatMessage.cs @@ -0,0 +1,11 @@ +using System; + +namespace LiveGameFeed.Models +{ + public class ChatMessage + { + public int MatchId { get; set; } + public string Text { get; set; } + public DateTime CreatedAt {get; set;} + } +} diff --git a/app/shared/components/chat.component.html b/app/shared/components/chat.component.html index dc0e19d..e0f5e86 100644 --- a/app/shared/components/chat.component.html +++ b/app/shared/components/chat.component.html @@ -10,29 +10,29 @@ {{match.Id}} {{match.Host}} vs {{match.Guest}} - + \ No newline at end of file diff --git a/app/shared/components/chat.component.ts b/app/shared/components/chat.component.ts index 65bbac9..f44dc4e 100644 --- a/app/shared/components/chat.component.ts +++ b/app/shared/components/chat.component.ts @@ -1,6 +1,7 @@ import { Component, OnInit, Input } from '@angular/core'; -import { Match } from '../interfaces'; +import { ChatMessage, Match } from '../interfaces'; +import { FeedService } from '../services/feed.service'; @Component({ selector: 'chat', @@ -9,8 +10,21 @@ import { Match } from '../interfaces'; export class ChatComponent implements OnInit { @Input() matches: Match[]; + messages: ChatMessage[]; - constructor() { } + constructor(private feedService: FeedService) { } - ngOnInit() { } + ngOnInit() { + let self = this; + + self.feedService.addChatMessage.subscribe( + message => { + console.log('received..'); + console.log(message); + if(!self.messages) + self.messages = new Array(); + self.messages.unshift(message); + } + ) + } } \ No newline at end of file diff --git a/app/shared/components/match.component.html b/app/shared/components/match.component.html index e1e7091..1c4ba0f 100644 --- a/app/shared/components/match.component.html +++ b/app/shared/components/match.component.html @@ -28,8 +28,8 @@ - - + + diff --git a/app/shared/components/match.component.ts b/app/shared/components/match.component.ts index 62c614f..345625a 100644 --- a/app/shared/components/match.component.ts +++ b/app/shared/components/match.component.ts @@ -1,7 +1,8 @@ import { Component, OnInit, Input } from '@angular/core'; -import { Match } from '../interfaces'; +import { ChatMessage, Match } from '../interfaces'; import { FeedService } from '../services/feed.service'; +import { DataService } from '../services/data.service'; @Component({ selector: 'match', @@ -11,8 +12,10 @@ export class MatchComponent implements OnInit { @Input() match: Match; subscribed: boolean; + chatMessage: string = ''; - constructor(private feedService: FeedService) { } + constructor(private feedService: FeedService, + private dataService: DataService) { } ngOnInit() { } @@ -26,4 +29,23 @@ export class MatchComponent implements OnInit { this.subscribed = false; this.feedService.unsubscribeToFeed(this.match.Id); } + + addChatMessage() { + let self = this; + let messageToSend: ChatMessage = { + MatchId : self.match.Id, + Text : self.chatMessage, + CreatedAt : new Date(Date.now()) + }; + + this.dataService.addChatMessage(messageToSend) + .subscribe(() => { + console.log('message sent..'); + }, + error => { + console.log(error); + }); + + self.chatMessage = ''; + } } \ No newline at end of file diff --git a/app/shared/interfaces.ts b/app/shared/interfaces.ts index 3bdd849..92f33b7 100644 --- a/app/shared/interfaces.ts +++ b/app/shared/interfaces.ts @@ -15,6 +15,7 @@ export interface FeedClient { updateMatch: (match: Match) => void; addFeed: (feed: Feed) => void; messageReceived: (message: string) => void; + addChatMessage: (chatMessage: ChatMessage) => void; } export interface FeedServer { @@ -45,4 +46,10 @@ export interface Feed { Description: string; CreatedAt: Date; MatchId: number; +} + +export interface ChatMessage { + MatchId: number; + Text: string; + CreatedAt: Date; } \ No newline at end of file diff --git a/app/shared/services/data.service.ts b/app/shared/services/data.service.ts index 380ec40..b01db76 100644 --- a/app/shared/services/data.service.ts +++ b/app/shared/services/data.service.ts @@ -6,7 +6,7 @@ import {Observer} from 'rxjs/Observer'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; -import { Match } from '../interfaces'; +import { ChatMessage, Match } from '../interfaces'; import { ConfigService } from './config.service'; @Injectable() @@ -25,6 +25,20 @@ export class DataService { .catch(this.handleError); } + addChatMessage(message: ChatMessage): Observable { + + let headers = new Headers(); + headers.append('Content-Type', 'application/json'); + + return this.http.post(this._baseUrl + 'messages/', JSON.stringify(message), { + headers: headers + }) + .map((res: Response) => { + return null; + }) + .catch(this.handleError); + } + private extractData(res: Response) { let body = res.json(); return body || {}; diff --git a/app/shared/services/feed.service.ts b/app/shared/services/feed.service.ts index 3580a08..908b339 100644 --- a/app/shared/services/feed.service.ts +++ b/app/shared/services/feed.service.ts @@ -5,7 +5,7 @@ import 'rxjs/add/operator/toPromise'; import { Observable } from "rxjs/Observable"; import { Subject } from "rxjs/Subject"; -import { FeedSignalR, FeedProxy, FeedClient, FeedServer, ConnectionState, Match, Feed } from '../interfaces'; +import { FeedSignalR, FeedProxy, FeedClient, FeedServer, ConnectionState, ChatMessage, Match, Feed } from '../interfaces'; @Injectable() export class FeedService { @@ -17,6 +17,7 @@ export class FeedService { updateMatch: Observable; addFeed: Observable; messageReceived: Observable; + addChatMessage: Observable; private connectionStateSubject = new Subject(); private userConnectedSubject = new Subject(); @@ -24,6 +25,7 @@ export class FeedService { private updateMatchSubject = new Subject(); private addFeedSubject = new Subject(); private messageReceivedSubject = new Subject(); + private addChatMessageSubject = new Subject(); private server: FeedServer; @@ -34,6 +36,7 @@ export class FeedService { this.updateMatch = this.updateMatchSubject.asObservable(); this.addFeed = this.addFeedSubject.asObservable(); this.messageReceived = this.messageReceivedSubject.asObservable(); + this.addChatMessage = this.addChatMessageSubject.asObservable(); } start(debug: boolean): Observable { @@ -67,6 +70,8 @@ export class FeedService { */ feedHub.client.messageReceived = message => this.onMessageReceived(message); + feedHub.client.addChatMessage = chatMessage => this.onAddChatMessage(chatMessage); + if (debug) { // for debug only, callback on connection state change $.connection.hub.stateChanged(change => { @@ -101,7 +106,6 @@ export class FeedService { } private onUserConnected(user: any) { - console.log("Chat Hub new user connected: " + user); this.userConnectedSubject.next(user); } @@ -110,14 +114,19 @@ export class FeedService { } private onAddFeed(feed: Feed) { + console.log(feed); this.addFeedSubject.next(feed); } private onMessageReceived(message: string) { - console.log(message); this.messageReceivedSubject.next(message); } + private onAddChatMessage(chatMessage: ChatMessage) { + console.log(chatMessage); + this.addChatMessageSubject.next(chatMessage); + } + public subscribeToFeed(matchId: number) { this.server.subscribe(matchId); }