mirror of
https://github.com/chsakell/aspnet-core-signalr-angular.git
synced 2025-12-23 01:37:59 +00:00
more refactoring in feed service
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { Component, OnInit } from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
|
||||||
import { FeedService } from './shared/services/feed.service';
|
import { FeedService } from './shared/services/feed.service';
|
||||||
import { ConnectionState } from './shared/interfaces';
|
import { SignalRConnectionStatus } from './shared/interfaces';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'my-app',
|
selector: 'my-app',
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { Component, OnInit } from '@angular/core';
|
|||||||
import { FeedService } from '../shared/services/feed.service';
|
import { FeedService } from '../shared/services/feed.service';
|
||||||
import { Match, Feed } from '../shared/interfaces';
|
import { Match, Feed } from '../shared/interfaces';
|
||||||
import { DataService } from '../shared/services/data.service';
|
import { DataService } from '../shared/services/data.service';
|
||||||
import { ConnectionState } from '../shared/interfaces';
|
import { SignalRConnectionStatus } from '../shared/interfaces';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'home',
|
selector: 'home',
|
||||||
@@ -23,7 +23,7 @@ export class HomeComponent implements OnInit {
|
|||||||
self.feedService.connectionState
|
self.feedService.connectionState
|
||||||
.subscribe(
|
.subscribe(
|
||||||
connectionState => {
|
connectionState => {
|
||||||
if (connectionState == ConnectionState.Connected) {
|
if (connectionState == SignalRConnectionStatus.Connected) {
|
||||||
console.log('Connected!');
|
console.log('Connected!');
|
||||||
self.loadMatches();
|
self.loadMatches();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ export interface FeedServer {
|
|||||||
unsubscribe(matchId: number): void;
|
unsubscribe(matchId: number): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum ConnectionState {
|
export enum SignalRConnectionStatus {
|
||||||
Connected = 1,
|
Connected = 1,
|
||||||
Disconnected = 2,
|
Disconnected = 2,
|
||||||
Error = 3
|
Error = 3
|
||||||
|
|||||||
@@ -5,20 +5,20 @@ 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, ChatMessage, Match, Feed } from '../interfaces';
|
import { FeedSignalR, FeedProxy, FeedClient, FeedServer, SignalRConnectionStatus, ChatMessage, Match, Feed } from '../interfaces';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class FeedService {
|
export class FeedService {
|
||||||
|
|
||||||
currentState = ConnectionState.Disconnected;
|
currentState = SignalRConnectionStatus.Disconnected;
|
||||||
connectionState: Observable<ConnectionState>;
|
connectionState: Observable<SignalRConnectionStatus>;
|
||||||
|
|
||||||
userConnected: Observable<any>;
|
userConnected: Observable<any>;
|
||||||
updateMatch: Observable<Match>;
|
updateMatch: Observable<Match>;
|
||||||
addFeed: Observable<Feed>;
|
addFeed: Observable<Feed>;
|
||||||
addChatMessage: Observable<ChatMessage>;
|
addChatMessage: Observable<ChatMessage>;
|
||||||
|
|
||||||
private connectionStateSubject = new Subject<ConnectionState>();
|
private connectionStateSubject = new Subject<SignalRConnectionStatus>();
|
||||||
private userConnectedSubject = new Subject<any>();
|
private userConnectedSubject = new Subject<any>();
|
||||||
|
|
||||||
private updateMatchSubject = new Subject<Match>();
|
private updateMatchSubject = new Subject<Match>();
|
||||||
@@ -36,7 +36,7 @@ export class FeedService {
|
|||||||
this.addChatMessage = this.addChatMessageSubject.asObservable();
|
this.addChatMessage = this.addChatMessageSubject.asObservable();
|
||||||
}
|
}
|
||||||
|
|
||||||
start(debug: boolean): Observable<ConnectionState> {
|
start(debug: boolean): Observable<SignalRConnectionStatus> {
|
||||||
// only for debug
|
// only for debug
|
||||||
$.connection.hub.logging = debug;
|
$.connection.hub.logging = debug;
|
||||||
// get the signalR hub named 'broadcaster'
|
// get the signalR hub named 'broadcaster'
|
||||||
@@ -64,13 +64,13 @@ export class FeedService {
|
|||||||
|
|
||||||
// start the connection
|
// start the connection
|
||||||
$.connection.hub.start()
|
$.connection.hub.start()
|
||||||
.done(response => this.setConnectionState(ConnectionState.Connected))
|
.done(response => this.setConnectionState(SignalRConnectionStatus.Connected))
|
||||||
.fail(error => this.connectionStateSubject.error(error));
|
.fail(error => this.connectionStateSubject.error(error));
|
||||||
|
|
||||||
return this.connectionState;
|
return this.connectionState;
|
||||||
}
|
}
|
||||||
|
|
||||||
private setConnectionState(connectionState: ConnectionState) {
|
private setConnectionState(connectionState: SignalRConnectionStatus) {
|
||||||
console.log('connection state changed to: ' + connectionState);
|
console.log('connection state changed to: ' + connectionState);
|
||||||
this.currentState = connectionState;
|
this.currentState = connectionState;
|
||||||
this.connectionStateSubject.next(connectionState);
|
this.connectionStateSubject.next(connectionState);
|
||||||
|
|||||||
Reference in New Issue
Block a user