first test with websockets (quick and dirty)

This commit is contained in:
Nicolas Constant 2018-03-22 00:31:34 -04:00
parent a52a26e6b5
commit 36657eb3cf
4 changed files with 34 additions and 3 deletions

View File

@ -111,6 +111,7 @@
<TypeScriptCompile Include="src\app\services\auth.service.ts" />
<TypeScriptCompile Include="src\app\services\models\api.settings.ts" />
<TypeScriptCompile Include="src\app\services\models\mastodon.interfaces.ts" />
<TypeScriptCompile Include="src\app\services\streaming.service.ts" />
<TypeScriptCompile Include="src\app\services\streams.service.ts" />
<TypeScriptCompile Include="src\environments\environment.prod.ts" />
<TypeScriptCompile Include="src\environments\environment.ts" />

View File

@ -16,6 +16,7 @@ import { RegisterNewAccountComponent } from "./pages/register-new-account/regist
import { AuthService } from "./services/auth.service";
import { AccountsService } from "./services/accounts.service";
import { StreamsService } from "./services/streams.service";
import { StreamingService } from "./services/streaming.service";
const routes: Routes = [
{ path: "", redirectTo: "home", pathMatch: "full" },
@ -41,7 +42,7 @@ const routes: Routes = [
NgxElectronModule,
RouterModule.forRoot(routes)
],
providers: [AuthService, AccountsService, StreamsService, { provide: APP_INITIALIZER, useFactory: settingsServiceFactory, deps: [AccountsService], multi: true }],
providers: [AuthService, AccountsService, StreamsService, StreamingService, { provide: APP_INITIALIZER, useFactory: settingsServiceFactory, deps: [AccountsService], multi: true }],
bootstrap: [AppComponent]
})
export class AppModule { }

View File

@ -5,6 +5,7 @@ import { AccountWrapper } from "./account.models";
import { LocalAccount } from "../services/accounts.service";
import { ApiRoutes } from "../services/models/api.settings";
import { Account, Status } from "../services/models/mastodon.interfaces";
import { StreamingService, StreamingWrapper } from "../services/streaming.service";
export class Stream {
private apiRoutes = new ApiRoutes();
@ -20,7 +21,13 @@ export class Stream {
this.retrieveToots(); //TODO change this for WebSockets
}
private test: StreamingWrapper;
private retrieveToots(): void {
//TEST
const service = new StreamingService();
this.test = service.getStreaming(this.account.mastodonInstance, this.account.tokenData.access_token);
//END TEST
const route = this.getTimelineRoute();
const header = new Headers();
@ -60,8 +67,6 @@ export enum StreamTypeEnum {
export class TootWrapper {
constructor(status: Status) {
console.warn(status);
this.account = new AccountWrapper();
this.account.username = status.account.username;
this.account.display_name = status.account.display_name;

View File

@ -0,0 +1,24 @@
import { Injectable } from "@angular/core";
@Injectable()
export class StreamingService {
constructor() { }
//TODO restructure this to handle real domain objects
getStreaming(mastodonInstance: string, accessToken: string): StreamingWrapper {
return new StreamingWrapper(mastodonInstance.replace("https://", "wss://") + `/api/v1/streaming//?access_token=${accessToken}&stream=public`)
}
}
export class StreamingWrapper {
constructor(private readonly domain: string) {
const eventSource = new WebSocket(domain);
eventSource.onmessage = x => console.warn(JSON.parse(x.data));
eventSource.onerror = x => console.error(x);
eventSource.onopen = x => console.log(x);
eventSource.onclose = x => console.log(x);
}
}