mirror of
				https://github.com/avatao-content/frontend-tutorial-framework
				synced 2025-11-04 13:52:55 +00:00 
			
		
		
		
	Implement writing to and reading from console from API
This commit is contained in:
		@@ -36,6 +36,9 @@ export const config = {
 | 
			
		||||
    route: 'messages',
 | 
			
		||||
    showNextButton: true
 | 
			
		||||
  },
 | 
			
		||||
  console: {
 | 
			
		||||
    defaultContent: ''
 | 
			
		||||
  },
 | 
			
		||||
  testmessenger: {
 | 
			
		||||
    route: 'testmessenger'
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										7
									
								
								src/app/console/console-command.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/app/console/console-command.ts
									
									
									
									
									
										Normal 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;
 | 
			
		||||
}
 | 
			
		||||
@@ -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>
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user