You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.1 KiB

import { Injectable } from '@angular/core';
import * as signalr from '@microsoft/signalr'
export const hubs: { [key: string]: string } = {};
@Injectable({
providedIn: 'root'
})
export class SignalrService {
public connect(hub: string | [string, string], handler?: { [key: string]: (...args: any[]) => void }): Promise<signalr.HubConnection> {
return new Promise((resolve, reject) => {
const token = localStorage.getItem('token');
if (token) {
const connection: signalr.HubConnection = new signalr.HubConnectionBuilder()
.withUrl(typeof hub == 'string' ?
`${hubs['default']}/signalr/${hub}` :
`${hubs[hub[0]]}/${hub[1]}`, {
accessTokenFactory: () => token
})
.withAutomaticReconnect({ nextRetryDelayInMilliseconds: () => 5000 })
.build();
for (const item in handler) {
connection.on(item, handler[item]);
}
connection
.start()
.then(() => resolve(connection))
.catch(e => reject(e));
}
else {
reject();
}
});
}
}