testing EventSource

This commit is contained in:
Nicolas Constant 2019-09-12 00:08:28 -04:00
parent 2d9b48dede
commit b28a023e47
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
1 changed files with 81 additions and 0 deletions

View File

@ -16,6 +16,10 @@ export class StreamingService {
private readonly mastodonService: MastodonService) { }
getStreaming(accountInfo: AccountInfo, stream: StreamElement): StreamingWrapper {
console.warn('EventSourceStreaminWrapper');
new EventSourceStreaminWrapper(accountInfo, stream);
return new StreamingWrapper(this.mastodonService, accountInfo, stream, this.nbStatusPerIteration);
}
}
@ -144,6 +148,83 @@ export class StreamingWrapper {
}
}
export class EventSourceStreaminWrapper {
eventSource: EventSource;
private apiRoutes = new ApiRoutes();
constructor(
private readonly account: AccountInfo,
private readonly stream: StreamElement
){
this.start();
}
private start(){
const route = this.getRoute();
this.eventSource = new EventSource(route);
this.eventSource.addEventListener('update', u => {
console.warn('update');
console.warn(u);
});
this.eventSource.addEventListener('delete', d => {
console.warn('delete');
console.warn(d);
});
this.eventSource.onmessage = x => {
console.log(x);
if(x.data !== ''){
this.onMessage(JSON.parse(x.data));
}
};
this.eventSource.onerror = x => {
this.onError(x);
};
console.warn('this.eventSource.CONNECTING');
console.warn(this.eventSource.CONNECTING);
console.warn('this.eventSource.OPEN');
console.warn(this.eventSource.OPEN);
}
private onMessage(data) {
console.warn('onMessage');
console.warn(data);
}
private onError(data) {
console.warn('onError');
console.warn(data);
}
private getRoute(): string {
const streamingRouteType = this.getStreamingRouteType(this.stream.type);
let route = `https://${this.account.instance}/api/v1/streaming/${streamingRouteType}?access_token=${this.account.token.access_token}`;
return route;
}
private getStreamingRouteType(type: StreamTypeEnum): string {
switch (type) {
case StreamTypeEnum.global:
return 'public';
case StreamTypeEnum.local:
return 'public/local';
case StreamTypeEnum.personnal:
return 'user';
case StreamTypeEnum.directmessages:
return 'direct';
case StreamTypeEnum.tag:
return 'hashtag?tag={0}';
case StreamTypeEnum.list:
return 'list?list={0}';
case StreamTypeEnum.directmessages:
return 'direct';
default:
throw Error('Not supported');
}
}
}
class WebSocketEvent {
event: string;
payload: any;