Implement writing to and reading from console from API

This commit is contained in:
Kristóf Tóth 2018-05-27 17:41:18 +02:00
parent 3456595c5a
commit 2f3ea52b8e
4 changed files with 41 additions and 3 deletions

View File

@ -36,6 +36,9 @@ export const config = {
route: 'messages',
showNextButton: true
},
console: {
defaultContent: ''
},
testmessenger: {
route: 'testmessenger'
}

View File

@ -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;
}

View File

@ -1,4 +1,4 @@
<!-- Copyright (C) 2018 Avatao.com Innovative Learning Kft.
All Rights Reserved. See LICENSE file for details. -->
<textarea class="tfw-console"></textarea>
<textarea [(ngModel)]="console_content" class="tfw-console"></textarea>

View File

@ -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<ConsoleCommand>('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
});
}
}