mirror of
https://github.com/avatao-content/frontend-tutorial-framework
synced 2025-07-15 07:16:24 +00:00
Move angular services to separate directory
This commit is contained in:
37
src/app/services/terminado.service.ts
Normal file
37
src/app/services/terminado.service.ts
Normal file
@ -0,0 +1,37 @@
|
||||
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();
|
||||
const wsproto = (location.protocol === 'https:') ? 'wss://' : 'ws://';
|
||||
this.ws = new WebSocket(wsproto + window.location.host + '/terminal');
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user