fix: 终端远程连接组件添加消息监听和发送句柄

This commit is contained in:
TsMask
2024-03-27 19:13:22 +08:00
parent f85535672e
commit e10a82ff35
3 changed files with 56 additions and 3 deletions

View File

@@ -7,7 +7,7 @@ import 'xterm/css/xterm.css';
import { RESULT_CODE_ERROR } from '@/constants/result-constants';
import { OptionsType, WS } from '@/plugins/ws-websocket';
const ws = new WS();
const emit = defineEmits(['connect', 'close']);
const emit = defineEmits(['connect', 'close', 'message']);
const props = defineProps({
/**终端ID必传 */
id: {
@@ -19,6 +19,11 @@ const props = defineProps({
type: String,
required: true,
},
/**禁止输入 */
disable: {
type: Boolean,
default: false,
},
/**初始发送命令 */
initCmd: {
type: [String, Boolean],
@@ -133,7 +138,7 @@ function handleRanderXterm(container: HTMLElement | undefined) {
scrollback: 1000,
scrollSensitivity: 15,
tabStopWidth: 4,
disableStdin: true, // 禁止输入
disableStdin: props.disable, // 禁止输入
});
// 挂载
xterm.open(container);
@@ -207,6 +212,7 @@ function wsClose(code: number) {
/**接收消息后回调 */
function wsMessage(res: Record<string, any>) {
emit('message', res);
// console.log('wsMessage', res);
const { code, requestId, data } = res;
if (code === RESULT_CODE_ERROR) {
@@ -245,6 +251,19 @@ onMounted(() => {
onBeforeUnmount(() => {
ws.close();
});
// 给组件设置属性 ref="xxxTerminal"
// setup内使用 const xxxTerminal = ref();
defineExpose({
/**发送方法 */
send: (data: string) => {
ws.send({
requestId: `telnet_${props.hostId}`,
type: 'telnet',
data: `${data}\r\n`,
});
},
});
</script>
<template>