diff --git a/src/app/components/editor/editor.component.ts b/src/app/components/editor/editor.component.ts index 29519d7..ed3d9e5 100644 --- a/src/app/components/editor/editor.component.ts +++ b/src/app/components/editor/editor.component.ts @@ -140,6 +140,10 @@ export class EditorComponent { private client: WebSocket | undefined; @Input() set idRoom(idRoom: string) { + if( idRoom === undefined){ + return; + } + this.client = new WebSocket(`ws://127.0.0.1:3000/live/${idRoom}`); this.client.addEventListener('open', async () => { let conn = new Connection(this.client!); @@ -161,7 +165,8 @@ export class EditorComponent { constructor( private router: Router, private backendService: BackendService - ) {} + ) { + } // Efface le contenu de l'éditeur clear(): void { @@ -169,7 +174,7 @@ export class EditorComponent { } async onCreateRoomButtonClicked() { - const idRoom = await this.backendService.createRoom(); + const idRoom = await this.backendService.createRoom(this.editorContent); await this.router.navigate([`./editor-live/${idRoom}`]); } diff --git a/src/app/services/backendService.service.ts b/src/app/services/backendService.service.ts index 491be1a..24e6d77 100644 --- a/src/app/services/backendService.service.ts +++ b/src/app/services/backendService.service.ts @@ -16,8 +16,14 @@ export class BackendService { constructor() {} - async createRoom() { - const reponse = await fetch(`${this.apiUrl}/live`, { method: 'POST' }); + async createRoom(code: string) { + const reponse = await fetch(`${this.apiUrl}/live`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify( {code} ) + }); return reponse.text(); }