added chat table

This commit is contained in:
chsakell
2016-10-03 16:30:49 +03:00
parent df79d53626
commit 2b7a82666b
9 changed files with 119 additions and 17 deletions

View File

@@ -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<Broadcaster>
{
public MessagesController(
IConnectionManager signalRConnectionManager)
: base(signalRConnectionManager)
{
}
// POST api/messages
[HttpPost]
public void Post([FromBody]ChatMessage message)
{
this.Clients.Group(message.MatchId.ToString()).addChatMessage(message);
}
}
}

11
Models/ChatMessage.cs Normal file
View File

@@ -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;}
}
}

View File

@@ -10,29 +10,29 @@
<td><span class="teamName">{{match.Id}}</span></td> <td><span class="teamName">{{match.Id}}</span></td>
<td><span class="teamScore"> {{match.Host}} vs {{match.Guest}} </span></td> <td><span class="teamScore"> {{match.Host}} vs {{match.Guest}} </span></td>
</tr> </tr>
<!--<tr> <tr>
<td colspan="2"> <td colspan="2">
<div class="feed-table"> <div class="feed-table">
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
<tr> <tr>
<th></th> <th></th>
<th>Update</th> <th>Message</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr *ngFor="let feed of match.Feeds" feedHighlight> <tr *ngFor="let message of messages" feedHighlight>
<td> <td>
<span class="feed-time">{{feed.CreatedAt | date:'shortTime' }}</span> <span class="feed-time">{{message.CreatedAt | date:'shortTime' }} [{{message.MatchId}}]</span>
</td> </td>
<td class="filterable-cell"> <td class="filterable-cell">
<span class="feed-update"> {{feed.Description}} </span> <span class="feed-update"> {{message.Text}} </span>
</td> </td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
</div> </div>
</td> </td>
</tr>--> </tr>
</tbody> </tbody>
</table> </table>

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 { ChatMessage, Match } from '../interfaces';
import { FeedService } from '../services/feed.service';
@Component({ @Component({
selector: 'chat', selector: 'chat',
@@ -9,8 +10,21 @@ import { Match } from '../interfaces';
export class ChatComponent implements OnInit { export class ChatComponent implements OnInit {
@Input() matches: Match[]; @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<ChatMessage>();
self.messages.unshift(message);
}
)
}
} }

View File

@@ -28,8 +28,8 @@
</td> </td>
</tr> </tr>
<tr *ngIf="subscribed"> <tr *ngIf="subscribed">
<td><button type="button" class="btn btn-default btn-md">Send</button></td> <td><button type="button" class="btn btn-default btn-md" (click)="addChatMessage()">Send</button></td>
<td><input type="text" class="form-control" placeholder="Type message.." /></td> <td><input type="text" class="form-control" placeholder="Type message.." [(ngModel)]="chatMessage" /></td>
</tr> </tr>
<tr> <tr>
<td colspan="2"> <td colspan="2">

View File

@@ -1,7 +1,8 @@
import { Component, OnInit, Input } from '@angular/core'; import { Component, OnInit, Input } from '@angular/core';
import { Match } from '../interfaces'; import { ChatMessage, Match } from '../interfaces';
import { FeedService } from '../services/feed.service'; import { FeedService } from '../services/feed.service';
import { DataService } from '../services/data.service';
@Component({ @Component({
selector: 'match', selector: 'match',
@@ -11,8 +12,10 @@ export class MatchComponent implements OnInit {
@Input() match: Match; @Input() match: Match;
subscribed: boolean; subscribed: boolean;
chatMessage: string = '';
constructor(private feedService: FeedService) { } constructor(private feedService: FeedService,
private dataService: DataService) { }
ngOnInit() { } ngOnInit() { }
@@ -26,4 +29,23 @@ export class MatchComponent implements OnInit {
this.subscribed = false; this.subscribed = false;
this.feedService.unsubscribeToFeed(this.match.Id); 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 = '';
}
} }

View File

@@ -15,6 +15,7 @@ export interface FeedClient {
updateMatch: (match: Match) => void; updateMatch: (match: Match) => void;
addFeed: (feed: Feed) => void; addFeed: (feed: Feed) => void;
messageReceived: (message: string) => void; messageReceived: (message: string) => void;
addChatMessage: (chatMessage: ChatMessage) => void;
} }
export interface FeedServer { export interface FeedServer {
@@ -46,3 +47,9 @@ export interface Feed {
CreatedAt: Date; CreatedAt: Date;
MatchId: number; MatchId: number;
} }
export interface ChatMessage {
MatchId: number;
Text: string;
CreatedAt: Date;
}

View File

@@ -6,7 +6,7 @@ import {Observer} from 'rxjs/Observer';
import 'rxjs/add/operator/map'; import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/catch';
import { Match } from '../interfaces'; import { ChatMessage, Match } from '../interfaces';
import { ConfigService } from './config.service'; import { ConfigService } from './config.service';
@Injectable() @Injectable()
@@ -25,6 +25,20 @@ export class DataService {
.catch(this.handleError); .catch(this.handleError);
} }
addChatMessage(message: ChatMessage): Observable<void> {
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) { private extractData(res: Response) {
let body = res.json(); let body = res.json();
return body || {}; return body || {};

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, FeedServer, ConnectionState, Match, Feed } from '../interfaces'; import { FeedSignalR, FeedProxy, FeedClient, FeedServer, ConnectionState, ChatMessage, Match, Feed } from '../interfaces';
@Injectable() @Injectable()
export class FeedService { export class FeedService {
@@ -17,6 +17,7 @@ export class FeedService {
updateMatch: Observable<Match>; updateMatch: Observable<Match>;
addFeed: Observable<Feed>; addFeed: Observable<Feed>;
messageReceived: Observable<string>; messageReceived: Observable<string>;
addChatMessage: Observable<ChatMessage>;
private connectionStateSubject = new Subject<ConnectionState>(); private connectionStateSubject = new Subject<ConnectionState>();
private userConnectedSubject = new Subject<any>(); private userConnectedSubject = new Subject<any>();
@@ -24,6 +25,7 @@ export class FeedService {
private updateMatchSubject = new Subject<Match>(); private updateMatchSubject = new Subject<Match>();
private addFeedSubject = new Subject<Feed>(); private addFeedSubject = new Subject<Feed>();
private messageReceivedSubject = new Subject<string>(); private messageReceivedSubject = new Subject<string>();
private addChatMessageSubject = new Subject<ChatMessage>();
private server: FeedServer; private server: FeedServer;
@@ -34,6 +36,7 @@ export class FeedService {
this.updateMatch = this.updateMatchSubject.asObservable(); this.updateMatch = this.updateMatchSubject.asObservable();
this.addFeed = this.addFeedSubject.asObservable(); this.addFeed = this.addFeedSubject.asObservable();
this.messageReceived = this.messageReceivedSubject.asObservable(); this.messageReceived = this.messageReceivedSubject.asObservable();
this.addChatMessage = this.addChatMessageSubject.asObservable();
} }
start(debug: boolean): Observable<ConnectionState> { start(debug: boolean): Observable<ConnectionState> {
@@ -67,6 +70,8 @@ export class FeedService {
*/ */
feedHub.client.messageReceived = message => this.onMessageReceived(message); feedHub.client.messageReceived = message => this.onMessageReceived(message);
feedHub.client.addChatMessage = chatMessage => this.onAddChatMessage(chatMessage);
if (debug) { if (debug) {
// for debug only, callback on connection state change // for debug only, callback on connection state change
$.connection.hub.stateChanged(change => { $.connection.hub.stateChanged(change => {
@@ -101,7 +106,6 @@ export class FeedService {
} }
private onUserConnected(user: any) { private onUserConnected(user: any) {
console.log("Chat Hub new user connected: " + user);
this.userConnectedSubject.next(user); this.userConnectedSubject.next(user);
} }
@@ -110,14 +114,19 @@ export class FeedService {
} }
private onAddFeed(feed: Feed) { private onAddFeed(feed: Feed) {
console.log(feed);
this.addFeedSubject.next(feed); this.addFeedSubject.next(feed);
} }
private onMessageReceived(message: string) { private onMessageReceived(message: string) {
console.log(message);
this.messageReceivedSubject.next(message); this.messageReceivedSubject.next(message);
} }
private onAddChatMessage(chatMessage: ChatMessage) {
console.log(chatMessage);
this.addChatMessageSubject.next(chatMessage);
}
public subscribeToFeed(matchId: number) { public subscribeToFeed(matchId: number) {
this.server.subscribe(matchId); this.server.subscribe(matchId);
} }