2018-01-17 16:24:56 +00:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Terminal } from 'xterm';
|
|
|
|
import * as fit from 'xterm/lib/addons/fit/fit';
|
|
|
|
import * as terminado from 'xterm/lib/addons/terminado/terminado';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class TerminadoService {
|
|
|
|
xterm: Terminal;
|
|
|
|
ws: WebSocket;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
Terminal.applyAddon(fit);
|
|
|
|
Terminal.applyAddon(terminado);
|
|
|
|
this.xterm = new Terminal();
|
2018-01-26 14:20:40 +00:00
|
|
|
const wsproto = (location.protocol === 'https:') ? 'wss://' : 'ws://';
|
2018-01-25 11:26:53 +00:00
|
|
|
this.ws = new WebSocket(wsproto + window.location.host + '/terminal');
|
2018-01-17 16:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
attach(element: HTMLElement) {
|
|
|
|
this.ws.onopen = () => {
|
|
|
|
(<any>this.xterm).terminadoAttach(this.ws);
|
|
|
|
this.xterm.open(element);
|
|
|
|
this.fit();
|
|
|
|
this.xterm.blur();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
detach() {
|
|
|
|
(<any>this.xterm).terminadoDetach(this.ws);
|
|
|
|
this.xterm.destroy();
|
|
|
|
this.ws.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
fit() {
|
|
|
|
(<any>this.xterm).fit();
|
|
|
|
}
|
|
|
|
}
|