Sengi-Windows-MacOS-Linux/src/app/services/streaming.service.ts

104 lines
2.8 KiB
TypeScript
Raw Normal View History

import { Injectable } from "@angular/core";
2018-09-13 08:02:24 +02:00
import { Status } from "./models/mastodon.interfaces";
import { BehaviorSubject } from "rxjs";
import { ApiRoutes } from "./models/api.settings";
2018-09-16 08:09:48 +02:00
import { StreamTypeEnum } from "../states/streams.state";
@Injectable()
export class StreamingService {
private apiRoutes = new ApiRoutes();
constructor() { }
2018-09-16 08:09:48 +02:00
getStreaming(instance: string, accessToken: string, streamType: StreamTypeEnum): StreamingWrapper {
const request = this.getRequest(streamType);
const route = `wss://${instance}/api/v1/streaming?access_token=${accessToken}&stream=${request}`
return new StreamingWrapper(route);
}
2018-09-16 08:09:48 +02:00
private getRequest(type: StreamTypeEnum): string {
switch (type) {
case StreamTypeEnum.global:
return 'public';
case StreamTypeEnum.local:
return 'public:local';
case StreamTypeEnum.personnal:
return 'user';
}
}
}
export class StreamingWrapper {
statusUpdateSubjet = new BehaviorSubject<StatusUpdate>(null);
eventSource: WebSocket;
constructor(private readonly domain: string) {
2018-09-16 08:09:48 +02:00
this.start(domain);
}
2018-09-13 08:02:24 +02:00
private start(domain: string) {
this.eventSource = new WebSocket(domain);
2018-09-16 08:09:48 +02:00
this.eventSource.onmessage = x => this.statusParsing(<WebSocketEvent>JSON.parse(x.data));
this.eventSource.onerror = x => this.webSocketGotError(x);
this.eventSource.onopen = x => console.log(x);
2018-09-16 08:09:48 +02:00
this.eventSource.onclose = x => this.webSocketClosed(domain, x);
}
private errorClosing: boolean;
private webSocketGotError(x: Event) {
console.error(x);
this.errorClosing = true;
// this.eventSource.close();
}
private webSocketClosed(domain, x: Event) {
console.log(x);
if(this.errorClosing){
this.errorClosing = false;
} else {
setTimeout(() => { this.start(domain) }, 3000);
}
}
2018-09-13 08:02:24 +02:00
2018-09-16 08:09:48 +02:00
private statusParsing(event: WebSocketEvent) {
const newUpdate = new StatusUpdate();
switch (event.event) {
case 'update':
newUpdate.type = EventEnum.update;
newUpdate.status = <Status>JSON.parse(event.payload);
break;
case 'delete':
newUpdate.type = EventEnum.delete;
newUpdate.messageId = event.payload;
break;
default:
newUpdate.type = EventEnum.unknow;
}
this.statusUpdateSubjet.next(newUpdate);
}
2018-09-16 08:09:48 +02:00
2018-09-13 08:02:24 +02:00
}
class WebSocketEvent {
event: string;
payload: any;
2018-09-13 08:02:24 +02:00
}
export class StatusUpdate {
type: EventEnum;
status: Status;
messageId: number;
2018-09-13 08:02:24 +02:00
}
export enum EventEnum {
unknow = 0,
update = 1,
delete = 2
}