73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
import { dom, library } from '@fortawesome/fontawesome-svg-core';
|
|
import { faCogs, faKeyboard } from '@fortawesome/free-solid-svg-icons';
|
|
import _ from 'lodash';
|
|
|
|
import '../assets/scss/styles.scss';
|
|
|
|
import { disconnect } from './wetty/disconnect';
|
|
import { overlay } from './wetty/disconnect/elements';
|
|
import { verifyPrompt } from './wetty/disconnect/verify';
|
|
import { FileDownloader } from './wetty/download';
|
|
import { mobileKeyboard } from './wetty/mobile';
|
|
import { socket } from './wetty/socket';
|
|
import { terminal, Term } from './wetty/term';
|
|
|
|
// Setup for fontawesome
|
|
library.add(faCogs);
|
|
library.add(faKeyboard);
|
|
dom.watch();
|
|
|
|
function onResize(term: Term): () => void {
|
|
return function resize() {
|
|
term.resizeTerm();
|
|
};
|
|
}
|
|
|
|
const term = terminal(socket);
|
|
if (_.isUndefined(term)) {
|
|
disconnect('终端初始化失败:未找到 #terminal 容器');
|
|
} else {
|
|
window.addEventListener('beforeunload', verifyPrompt, false);
|
|
window.addEventListener('resize', onResize(term), false);
|
|
|
|
term.resizeTerm();
|
|
term.focus();
|
|
mobileKeyboard();
|
|
const fileDownloader = new FileDownloader();
|
|
|
|
socket
|
|
.on('connect', () => {
|
|
if (!_.isNull(overlay)) overlay.style.display = 'none';
|
|
})
|
|
.on('data', (data: string) => {
|
|
const remainingData = fileDownloader.buffer(data);
|
|
if (remainingData) {
|
|
term.write(remainingData);
|
|
}
|
|
})
|
|
.on('login', () => {
|
|
if (!_.isNull(overlay)) overlay.style.display = 'none';
|
|
term.writeln('');
|
|
term.resizeTerm();
|
|
term.focus();
|
|
})
|
|
.on('logout', disconnect)
|
|
.on('disconnect', disconnect)
|
|
.on('error', (err: string | null) => {
|
|
if (err) disconnect(err);
|
|
});
|
|
|
|
term.onData((data: string) => {
|
|
socket.emit('input', data);
|
|
});
|
|
term.onResize((size: { cols: number; rows: number }) => {
|
|
socket.emit('resize', size);
|
|
});
|
|
|
|
socket.connect().catch((error: unknown) => {
|
|
const message =
|
|
error instanceof Error ? error.message : '连接网关失败,请检查配置';
|
|
disconnect(message);
|
|
});
|
|
}
|