31 lines
735 B
TypeScript
31 lines
735 B
TypeScript
import { Injectable,MessageEvent } from '@nestjs/common';
|
|
import { Subject } from 'rxjs';
|
|
import { Key } from 'src/model/entitites';
|
|
|
|
@Injectable()
|
|
export class SseService {
|
|
private clients = new Map<string, Subject<MessageEvent>>();
|
|
|
|
sendKeysToUsers(userId: string, keys: Key[]) {
|
|
try {
|
|
const sub = this.clients.get(userId);
|
|
if (!sub) { return; }
|
|
sub.next({ data: keys })
|
|
} catch {}
|
|
}
|
|
|
|
register(userId: string) {
|
|
const subj = new Subject<MessageEvent>();
|
|
this.clients.set(userId, subj);
|
|
return subj;
|
|
}
|
|
|
|
unregister(userId: string) {
|
|
if (!this.clients.has(userId)) { return; }
|
|
|
|
const sub = this.clients.get(userId);
|
|
sub.unsubscribe();
|
|
this.clients.delete(userId);
|
|
}
|
|
}
|