mirror of
https://github.com/chsakell/aspnet-core-signalr-angular.git
synced 2025-12-22 17:27:48 +00:00
refactor feed service
This commit is contained in:
@@ -9,10 +9,6 @@ namespace LiveGameFeed.Hubs
|
|||||||
{
|
{
|
||||||
return Clients.All.userConnected("New connection " + Context.ConnectionId);
|
return Clients.All.userConnected("New connection " + Context.ConnectionId);
|
||||||
}
|
}
|
||||||
public Task Broadcast(string message)
|
|
||||||
{
|
|
||||||
return Clients.All.messageReceived(Context.ConnectionId + "> " + message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task Subscribe(int matchId)
|
public Task Subscribe(int matchId)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ export interface FeedClient {
|
|||||||
|
|
||||||
updateMatch: (match: Match) => void;
|
updateMatch: (match: Match) => void;
|
||||||
addFeed: (feed: Feed) => void;
|
addFeed: (feed: Feed) => void;
|
||||||
messageReceived: (message: string) => void;
|
|
||||||
addChatMessage: (chatMessage: ChatMessage) => void;
|
addChatMessage: (chatMessage: ChatMessage) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,11 +12,10 @@ export class FeedService {
|
|||||||
|
|
||||||
currentState = ConnectionState.Disconnected;
|
currentState = ConnectionState.Disconnected;
|
||||||
connectionState: Observable<ConnectionState>;
|
connectionState: Observable<ConnectionState>;
|
||||||
userConnected: Observable<any>;
|
|
||||||
|
|
||||||
|
userConnected: Observable<any>;
|
||||||
updateMatch: Observable<Match>;
|
updateMatch: Observable<Match>;
|
||||||
addFeed: Observable<Feed>;
|
addFeed: Observable<Feed>;
|
||||||
messageReceived: Observable<string>;
|
|
||||||
addChatMessage: Observable<ChatMessage>;
|
addChatMessage: Observable<ChatMessage>;
|
||||||
|
|
||||||
private connectionStateSubject = new Subject<ConnectionState>();
|
private connectionStateSubject = new Subject<ConnectionState>();
|
||||||
@@ -24,18 +23,16 @@ 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 addChatMessageSubject = new Subject<ChatMessage>();
|
private addChatMessageSubject = new Subject<ChatMessage>();
|
||||||
|
|
||||||
private server: FeedServer;
|
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();
|
||||||
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.addChatMessage = this.addChatMessageSubject.asObservable();
|
this.addChatMessage = this.addChatMessageSubject.asObservable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,34 +60,8 @@ export class FeedService {
|
|||||||
*/
|
*/
|
||||||
feedHub.client.addFeed = feed => this.onAddFeed(feed);
|
feedHub.client.addFeed = feed => this.onAddFeed(feed);
|
||||||
|
|
||||||
/**
|
|
||||||
* @desc callback when a message is received
|
|
||||||
* @param String to, the conversation id
|
|
||||||
* @param Message data, the message
|
|
||||||
*/
|
|
||||||
feedHub.client.messageReceived = message => this.onMessageReceived(message);
|
|
||||||
|
|
||||||
feedHub.client.addChatMessage = chatMessage => this.onAddChatMessage(chatMessage);
|
feedHub.client.addChatMessage = chatMessage => this.onAddChatMessage(chatMessage);
|
||||||
|
|
||||||
if (debug) {
|
|
||||||
// for debug only, callback on connection state change
|
|
||||||
$.connection.hub.stateChanged(change => {
|
|
||||||
let oldState: string,
|
|
||||||
newState: string;
|
|
||||||
|
|
||||||
for (var state in $.signalR.connectionState) {
|
|
||||||
if ($.signalR.connectionState[state] === change.oldState) {
|
|
||||||
oldState = state;
|
|
||||||
}
|
|
||||||
if ($.signalR.connectionState[state] === change.newState) {
|
|
||||||
newState = state;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("Feed Hub state changed from " + oldState + " to " + newState);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// start the connection
|
// start the connection
|
||||||
$.connection.hub.start()
|
$.connection.hub.start()
|
||||||
.done(response => this.setConnectionState(ConnectionState.Connected))
|
.done(response => this.setConnectionState(ConnectionState.Connected))
|
||||||
@@ -105,6 +76,7 @@ export class FeedService {
|
|||||||
this.connectionStateSubject.next(connectionState);
|
this.connectionStateSubject.next(connectionState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Client side methods
|
||||||
private onUserConnected(user: any) {
|
private onUserConnected(user: any) {
|
||||||
this.userConnectedSubject.next(user);
|
this.userConnectedSubject.next(user);
|
||||||
}
|
}
|
||||||
@@ -118,15 +90,12 @@ export class FeedService {
|
|||||||
this.addFeedSubject.next(feed);
|
this.addFeedSubject.next(feed);
|
||||||
}
|
}
|
||||||
|
|
||||||
private onMessageReceived(message: string) {
|
|
||||||
this.messageReceivedSubject.next(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
private onAddChatMessage(chatMessage: ChatMessage) {
|
private onAddChatMessage(chatMessage: ChatMessage) {
|
||||||
console.log(chatMessage);
|
console.log(chatMessage);
|
||||||
this.addChatMessageSubject.next(chatMessage);
|
this.addChatMessageSubject.next(chatMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Server side methods
|
||||||
public subscribeToFeed(matchId: number) {
|
public subscribeToFeed(matchId: number) {
|
||||||
this.server.subscribe(matchId);
|
this.server.subscribe(matchId);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user