From 2f3ea52b8e1ac0e89f75e80a5fc77167c930cc5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Sun, 27 May 2018 17:41:18 +0200 Subject: [PATCH] Implement writing to and reading from console from API --- src/app/config.ts | 3 +++ src/app/console/console-command.ts | 7 ++++++ src/app/console/console.component.html | 2 +- src/app/console/console.component.ts | 32 ++++++++++++++++++++++++-- 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 src/app/console/console-command.ts diff --git a/src/app/config.ts b/src/app/config.ts index 3df43bb..2579150 100644 --- a/src/app/config.ts +++ b/src/app/config.ts @@ -36,6 +36,9 @@ export const config = { route: 'messages', showNextButton: true }, + console: { + defaultContent: '' + }, testmessenger: { route: 'testmessenger' } diff --git a/src/app/console/console-command.ts b/src/app/console/console-command.ts new file mode 100644 index 0000000..7f05c2b --- /dev/null +++ b/src/app/console/console-command.ts @@ -0,0 +1,7 @@ +// Copyright (C) 2018 Avatao.com Innovative Learning Kft. +// All Rights Reserved. See LICENSE file for details. + +export class ConsoleCommand { + command: string; + content?: string; +} diff --git a/src/app/console/console.component.html b/src/app/console/console.component.html index cfa8363..cd6f85f 100644 --- a/src/app/console/console.component.html +++ b/src/app/console/console.component.html @@ -1,4 +1,4 @@ - + diff --git a/src/app/console/console.component.ts b/src/app/console/console.component.ts index 6455c32..e0d18be 100644 --- a/src/app/console/console.component.ts +++ b/src/app/console/console.component.ts @@ -2,6 +2,9 @@ // All Rights Reserved. See LICENSE file for details. import { Component, OnInit } from '@angular/core'; +import { WebSocketService } from '../services/websocket.service'; +import { ConsoleCommand } from './console-command'; +import { config } from '../config'; @Component({ selector: 'app-console', @@ -9,9 +12,34 @@ import { Component, OnInit } from '@angular/core'; styleUrls: ['./console.component.scss'] }) export class ConsoleComponent implements OnInit { + console_content: string = config.console.defaultContent; - constructor() {} + command_handlers = { + 'write': this.writeHandler.bind(this), + 'read': this.readHandler.bind(this) + }; - ngOnInit() {} + constructor(private webSocketService: WebSocketService) {} + ngOnInit() { + this.webSocketService.connect(); + this.webSocketService.observeKey('console').subscribe((event) => { + this.command_handlers[event.data.command](event.data); + }); + } + + writeHandler(data: ConsoleCommand) { + this.console_content = data.content; + } + + readHandler(data: ConsoleCommand) { + this.sendContent(this.console_content); + } + + sendContent(content: string) { + this.webSocketService.send('console', { + 'command': 'read', + 'content': content + }); + } }