Last active
November 12, 2019 21:24
-
-
Save nikneem/d878426150207d0056e5e18775e95933 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import * as signalR from '@aspnet/signalr'; | |
import { PictureDto } from '@store/pictures/pictures.models'; | |
@Injectable({ | |
providedIn: 'root', | |
}) | |
export class RealtimeService { | |
private connection: signalR.HubConnection; | |
constructor() {} | |
connectSignalR() { | |
if ( | |
!this.connection || | |
this.connection.state === signalR.HubConnectionState.Disconnected | |
) { | |
this.connection = new signalR.HubConnectionBuilder() | |
.configureLogging(signalR.LogLevel.Information) | |
.withUrl(`http://localhost:7071/api`) | |
.build(); | |
this.connection.on('newPicture', (dto: PictureDto) => { | |
console.log('New picture arrived'); | |
}); | |
this.connection | |
.start() | |
.then(function() {}) | |
.catch(function(err) { | |
return console.error(err.toString()); | |
}); | |
} | |
} | |
disconnectSignalR() { | |
if ( | |
this.connection && | |
this.connection.state === signalR.HubConnectionState.Connected | |
) { | |
this.connection.stop(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment