Update web-console-full-window.component.ts

This commit is contained in:
piotrpekala7
2020-05-05 17:05:12 +02:00
parent 467cf2b1d8
commit 626769aa61

View File

@ -18,7 +18,7 @@ import { NodeService } from '../../services/node.service';
templateUrl: './web-console-full-window.component.html', templateUrl: './web-console-full-window.component.html',
styleUrls: ['../../../../node_modules/xterm/css/xterm.css'] styleUrls: ['../../../../node_modules/xterm/css/xterm.css']
}) })
export class WebConsoleFullWindowComponent implements OnInit, AfterViewInit { export class WebConsoleFullWindowComponent implements OnInit {
private serverId: string; private serverId: string;
private projectId: string; private projectId: string;
private nodeId: string; private nodeId: string;
@ -53,44 +53,47 @@ export class WebConsoleFullWindowComponent implements OnInit, AfterViewInit {
this.nodeService.getNodeById(this.server, this.projectId, this.nodeId).subscribe((node: Node) => { this.nodeService.getNodeById(this.server, this.projectId, this.nodeId).subscribe((node: Node) => {
this.node = node; this.node = node;
this.title.setTitle(this.node.name); this.title.setTitle(this.node.name);
this.openTerminal();
}); });
}) })
} }
async ngAfterViewInit() { openTerminal() {
this.term.open(this.terminal.nativeElement); setTimeout(() => {
const socket = new WebSocket(await this.getUrl()); this.term.open(this.terminal.nativeElement);
const socket = new WebSocket(this.getUrl());
socket.onerror = ((event) => { socket.onerror = ((event) => {
this.term.write("Connection lost" + "\r\n"); this.term.write("Connection lost" + "\r\n");
}); });
socket.onclose = ((event) => { socket.onclose = ((event) => {
this.term.write("Connection closed" + "\r\n"); this.term.write("Connection closed" + "\r\n");
}); });
const attachAddon = new AttachAddon(socket); const attachAddon = new AttachAddon(socket);
this.term.loadAddon(attachAddon); this.term.loadAddon(attachAddon);
this.term.setOption('cursorBlink', true); this.term.setOption('cursorBlink', true);
this.term.loadAddon(this.fitAddon); this.term.loadAddon(this.fitAddon);
this.fitAddon.activate(this.term); this.fitAddon.activate(this.term);
this.fitAddon.fit(); this.fitAddon.fit();
this.term.focus(); this.term.focus();
this.term.attachCustomKeyEventHandler((key: KeyboardEvent) => { this.term.attachCustomKeyEventHandler((key: KeyboardEvent) => {
if (key.code === 'KeyC' || key.code === 'KeyV'){ if (key.code === 'KeyC' || key.code === 'KeyV'){
if (key.ctrlKey) { if (key.ctrlKey) {
return false; return false;
} }
} }
return true; return true;
}); });
let numberOfColumns = Math.round(window.innerWidth / this.consoleService.getLineWidth()); let numberOfColumns = Math.round(window.innerWidth / this.consoleService.getLineWidth());
let numberOfRows = Math.round(window.innerHeight / this.consoleService.getLineHeight()); let numberOfRows = Math.round(window.innerHeight / this.consoleService.getLineHeight());
this.term.resize(numberOfColumns, numberOfRows); this.term.resize(numberOfColumns, numberOfRows);
}, 0 );
} }
async getUrl() { getUrl() {
return `ws://${this.server.host}:${this.server.port}/v2/projects/${this.projectId}/nodes/${this.nodeId}/console/ws` return `ws://${this.server.host}:${this.server.port}/v2/projects/${this.projectId}/nodes/${this.nodeId}/console/ws`
} }
} }