Skip to content

Instantly share code, notes, and snippets.

@nikneem
Last active November 12, 2019 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikneem/d878426150207d0056e5e18775e95933 to your computer and use it in GitHub Desktop.
Save nikneem/d878426150207d0056e5e18775e95933 to your computer and use it in GitHub Desktop.
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