Cześć staram się napisać prosty chat korzystając z signalR oraz angular oraz .net core web api. Podczas dodawania wiadomości mam coś takiego :
await db.Messages.AddAsync(message);
await hubContext.Clients.All.BroadcastMessage(message);
await CommitAsync();
signlaR.service.ts
import { Injectable, EventEmitter } from '@angular/core';
import * as signalR from '@aspnet/signalr';
import { Message } from '@angular/compiler/src/i18n/i18n_ast';
@Injectable({
providedIn: 'root',
})
export class SignalRService {
private hubConnection: signalR.HubConnection;
signalReceived = new EventEmitter<Message>();
constructor() {
this.buildConnection();
this.startConnection();
}
private buildConnection = () => {
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl('https://localhost:5001/chat')
.build();
};
private startConnection = () => {
this.hubConnection
.start()
.then(() => {
console.log('Connection Started...');
this.registerSignalEvents();
})
.catch((err) => {
console.log('Error while starting connection: ' + err);
//if you get error try to start connection again after 3 seconds.
setTimeout(function () {
this.startConnection();
}, 3000);
});
};
private registerSignalEvents() {
this.hubConnection.on('BroadcastMessage', (data: Message) => {
this.signalReceived.emit(data);
});
}
}
Jak moge ograniczyć to do konkretnych użytkowników.
hubContext.Clients.All.Users() <--- nie działa. Z góry bardzo dziękuje za pomoc :)