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>
<body>
<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="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=wsijQt8sLXrCW71YesmispvYHitfG9gv&s=1"></script> -->
</body>
</html>

View File

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

View File

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

View File

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

View File

@@ -14,6 +14,7 @@ import (
const (
Resize = 1
Data = 2
Ping = 3
)
type TerminalSession struct {
@@ -64,6 +65,9 @@ func (r TerminalSession) Stop() {
global.Log.Debug("close machine ssh terminal session")
r.tick.Stop()
r.cancel()
if r.wsConn != nil {
r.wsConn.Close()
}
if r.terminal != nil {
if err := r.terminal.Close(); err != nil {
global.Log.Errorf("关闭机器ssh终端失败: %s", err.Error())
@@ -155,6 +159,12 @@ func (ts *TerminalSession) receiveWsMsg() {
if err != nil {
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
}
}
}
}