fix: 终端断连提示

This commit is contained in:
meilin.huang
2022-08-19 21:42:26 +08:00
parent 37ed5134e8
commit 226bb8f089
5 changed files with 48 additions and 34 deletions

View File

@@ -18,8 +18,7 @@
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>
<script type="text/javascript" src="./config.js"></script> <script type="module" src="./config.js"></script>
<script type="module" src="/src/main.ts"></script> <script type="module" src="/src/main.ts"></script>
<!-- <script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=wsijQt8sLXrCW71YesmispvYHitfG9gv&s=1"></script> -->
</body> </body>
</html> </html>

View File

@@ -28,7 +28,6 @@ export async function RsaEncrypt(value: any) {
if (encryptor != null) { if (encryptor != null) {
return encryptor.encrypt(value) return encryptor.encrypt(value)
} }
console.log(value)
encryptor = new JSEncrypt() encryptor = new JSEncrypt()
const publicKey = await getRsaPublicKey() as string; const publicKey = await getRsaPublicKey() as string;
notBlank(publicKey, "获取公钥失败") notBlank(publicKey, "获取公钥失败")

View File

@@ -165,7 +165,7 @@
size="small" size="small"
> >
<template #prepend> <template #prepend>
<el-popover v-model:visible="dt.selectColumnPopoverVisible" :width="320" placement="right"> <el-popover :visible="dt.selectColumnPopoverVisible" :width="320" placement="right">
<template #reference> <template #reference>
<el-link <el-link
@click="dt.selectColumnPopoverVisible = !dt.selectColumnPopoverVisible" @click="dt.selectColumnPopoverVisible = !dt.selectColumnPopoverVisible"

View File

@@ -29,6 +29,7 @@ export default defineComponent({
const resize = 1; const resize = 1;
const data = 2; const data = 2;
const ping = 3;
watch(props, (newValue) => { watch(props, (newValue) => {
state.machineId = newValue.machineId; state.machineId = newValue.machineId;
@@ -115,52 +116,50 @@ export default defineComponent({
}); });
} }
let pingInterval: any;
function initSocket() { function initSocket() {
state.socket = new WebSocket( state.socket = new WebSocket(
`${config.baseWsUrl}/machines/${state.machineId}/terminal?token=${getSession('token')}&cols=${state.term.cols}&rows=${ `${config.baseWsUrl}/machines/${state.machineId}/terminal?token=${getSession('token')}&cols=${state.term.cols}&rows=${
state.term.rows state.term.rows
}` }`
); );
// 监听socket连接 // 监听socket连接
state.socket.onopen = open; state.socket.onopen = () => {
// 如果有初始要执行的命令,则发送执行命令
if (state.cmd) {
sendCmd(state.cmd + ' \r');
}
// 开启心跳
pingInterval = setInterval(() => {
send({ type: ping, msg: 'ping' });
}, 8000);
};
// 监听socket错误信息 // 监听socket错误信息
state.socket.onerror = error; state.socket.onerror = (e: any) => {
// 监听socket消息 console.log('连接错误', e);
state.socket.onmessage = getMessage; };
state.socket.onclose = () => {
if (state.term) {
state.term.writeln('\r\n\x1b[31m提示: 连接已关闭...');
}
if (pingInterval) {
clearInterval(pingInterval);
}
};
// 发送socket消息 // 发送socket消息
state.socket.onsend = send; state.socket.onsend = send;
}
function open() { // 监听socket消息
// 如果有初始要执行的命令,则发送执行命令 state.socket.onmessage = getMessage;
if (state.cmd) {
sendCmd(state.cmd + ' \r');
}
//开启心跳
// this.start();
}
function error() {
console.log('连接错误');
//重连
// reconnect();
}
function close() {
if (state.socket) {
state.socket.close();
console.log('socket关闭');
}
//重连
// this.reconnect()
} }
function getMessage(msg: any) { function getMessage(msg: any) {
// msg.data是真正后端返回的数据 // msg.data是真正后端返回的数据
state.term.write(msg.data); state.term.write(msg.data);
//收到服务器信息,心跳重置
// this.reset();
} }
function send(msg: any) { function send(msg: any) {
@@ -174,6 +173,13 @@ export default defineComponent({
}); });
} }
function close() {
if (state.socket) {
state.socket.close();
console.log('socket关闭');
}
}
function closeAll() { function closeAll() {
close(); close();
if (state.term) { if (state.term) {

View File

@@ -14,6 +14,7 @@ import (
const ( const (
Resize = 1 Resize = 1
Data = 2 Data = 2
Ping = 3
) )
type TerminalSession struct { type TerminalSession struct {
@@ -64,6 +65,9 @@ func (r TerminalSession) Stop() {
global.Log.Debug("close machine ssh terminal session") global.Log.Debug("close machine ssh terminal session")
r.tick.Stop() r.tick.Stop()
r.cancel() r.cancel()
if r.wsConn != nil {
r.wsConn.Close()
}
if r.terminal != nil { if r.terminal != nil {
if err := r.terminal.Close(); err != nil { if err := r.terminal.Close(); err != nil {
global.Log.Errorf("关闭机器ssh终端失败: %s", err.Error()) global.Log.Errorf("关闭机器ssh终端失败: %s", err.Error())
@@ -155,6 +159,12 @@ func (ts *TerminalSession) receiveWsMsg() {
if err != nil { if err != nil {
global.Log.Debug("机器ssh终端写入消息失败: %s", err) global.Log.Debug("机器ssh终端写入消息失败: %s", err)
} }
case Ping:
_, err := ts.terminal.SshSession.SendRequest("ping", true, nil)
if err != nil {
WriteMessage(wsConn, "\r\n\033[1;31m提示: 终端连接已断开...\033[0m")
return
}
} }
} }
} }