2018-03-22 05:31:34 +01:00
|
|
|
import { Injectable } from "@angular/core";
|
2018-09-13 08:02:24 +02:00
|
|
|
import { Status } from "./models/mastodon.interfaces";
|
2018-03-22 05:31:34 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class StreamingService {
|
|
|
|
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
//TODO restructure this to handle real domain objects
|
2018-09-13 06:50:27 +02:00
|
|
|
getStreaming(instance: string, accessToken: string): StreamingWrapper {
|
|
|
|
const route = `wss://${instance}/api/v1/streaming?access_token=${accessToken}&stream=public`
|
|
|
|
return new StreamingWrapper(route);
|
2018-03-22 05:31:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export class StreamingWrapper {
|
|
|
|
|
|
|
|
constructor(private readonly domain: string) {
|
|
|
|
const eventSource = new WebSocket(domain);
|
2018-09-13 08:02:24 +02:00
|
|
|
eventSource.onmessage = x => this.tootParsing(<WebSocketEvent>JSON.parse(x.data));
|
2018-03-22 05:31:34 +01:00
|
|
|
eventSource.onerror = x => console.error(x);
|
|
|
|
eventSource.onopen = x => console.log(x);
|
|
|
|
eventSource.onclose = x => console.log(x);
|
|
|
|
}
|
2018-09-13 08:02:24 +02:00
|
|
|
|
|
|
|
tootParsing(event: WebSocketEvent){
|
|
|
|
console.warn(event.event);
|
|
|
|
console.warn(event.payload);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class WebSocketEvent {
|
|
|
|
event: string;
|
|
|
|
payload: Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class StatusUpdate {
|
|
|
|
type: EventEnum;
|
|
|
|
status: Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum EventEnum {
|
|
|
|
unknow = 0,
|
|
|
|
update = 1,
|
|
|
|
delete = 2
|
2018-03-22 05:31:34 +01:00
|
|
|
}
|