mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-02-24 23:04:40 +08:00
feat: 添加终端心跳包,增强连接稳定性,并为后续延迟检测做准备
This commit is contained in:
parent
77c0eb99f0
commit
6fb1e690aa
2 changed files with 32 additions and 6 deletions
|
@ -37,6 +37,7 @@ func (w *safeBuffer) Reset() {
|
||||||
const (
|
const (
|
||||||
wsMsgCmd = "cmd"
|
wsMsgCmd = "cmd"
|
||||||
wsMsgResize = "resize"
|
wsMsgResize = "resize"
|
||||||
|
wsMsgHeartbeat = "heartbeat"
|
||||||
)
|
)
|
||||||
|
|
||||||
type wsMsg struct {
|
type wsMsg struct {
|
||||||
|
@ -44,6 +45,7 @@ type wsMsg struct {
|
||||||
Data string `json:"data,omitempty"` // wsMsgCmd
|
Data string `json:"data,omitempty"` // wsMsgCmd
|
||||||
Cols int `json:"cols,omitempty"` // wsMsgResize
|
Cols int `json:"cols,omitempty"` // wsMsgResize
|
||||||
Rows int `json:"rows,omitempty"` // wsMsgResize
|
Rows int `json:"rows,omitempty"` // wsMsgResize
|
||||||
|
Timestamp int `json:"timestamp,omitempty"` // wsMsgHeartbeat
|
||||||
}
|
}
|
||||||
|
|
||||||
type LogicSshWsSession struct {
|
type LogicSshWsSession struct {
|
||||||
|
@ -140,6 +142,12 @@ func (sws *LogicSshWsSession) receiveWsMsg(exitCh chan bool) {
|
||||||
global.LOG.Errorf("websock cmd string base64 decoding failed, err: %v", err)
|
global.LOG.Errorf("websock cmd string base64 decoding failed, err: %v", err)
|
||||||
}
|
}
|
||||||
sws.sendWebsocketInputCommandToSshSessionStdinPipe(decodeBytes)
|
sws.sendWebsocketInputCommandToSshSessionStdinPipe(decodeBytes)
|
||||||
|
case wsMsgHeartbeat:
|
||||||
|
// 接收到心跳包后将心跳包原样返回,可以用于网络延迟检测等情况
|
||||||
|
err = wsConn.WriteMessage(websocket.TextMessage, wsData)
|
||||||
|
if err != nil {
|
||||||
|
global.LOG.Errorf("ssh sending heartbeat to webSocket failed, err: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@ const webSocketReady = ref(false);
|
||||||
const termReady = ref(false);
|
const termReady = ref(false);
|
||||||
const terminalSocket = ref<WebSocket>();
|
const terminalSocket = ref<WebSocket>();
|
||||||
const term = ref<Terminal>();
|
const term = ref<Terminal>();
|
||||||
|
const heartbeatTimer = ref<number>();
|
||||||
|
|
||||||
const readyWatcher = watch(
|
const readyWatcher = watch(
|
||||||
() => webSocketReady.value && termReady.value,
|
() => webSocketReady.value && termReady.value,
|
||||||
|
@ -59,6 +60,10 @@ const onWSReceive = (message: MessageEvent) => {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 'heartbeat': {
|
||||||
|
console.debug('latency', new Date().getTime() - wsMsg.timestamp, 'ms');
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -71,6 +76,9 @@ const errorRealTerminal = (ex: any) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const closeRealTerminal = (ev: CloseEvent) => {
|
const closeRealTerminal = (ev: CloseEvent) => {
|
||||||
|
if (heartbeatTimer.value) {
|
||||||
|
clearInterval(heartbeatTimer.value);
|
||||||
|
}
|
||||||
if (term.value) {
|
if (term.value) {
|
||||||
term.value.write(ev.reason);
|
term.value.write(ev.reason);
|
||||||
}
|
}
|
||||||
|
@ -125,6 +133,16 @@ const initTerm = () => {
|
||||||
terminalSocket.value.onmessage = onWSReceive;
|
terminalSocket.value.onmessage = onWSReceive;
|
||||||
terminalSocket.value.onclose = closeRealTerminal;
|
terminalSocket.value.onclose = closeRealTerminal;
|
||||||
terminalSocket.value.onerror = errorRealTerminal;
|
terminalSocket.value.onerror = errorRealTerminal;
|
||||||
|
heartbeatTimer.value = setInterval(() => {
|
||||||
|
if (isWsOpen()) {
|
||||||
|
terminalSocket.value!.send(
|
||||||
|
JSON.stringify({
|
||||||
|
type: 'heartbeat',
|
||||||
|
timestamp: `${new Date().getTime()}`,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}, 1000 * 10);
|
||||||
term.value.onData((data: any) => {
|
term.value.onData((data: any) => {
|
||||||
if (isWsOpen()) {
|
if (isWsOpen()) {
|
||||||
terminalSocket.value!.send(
|
terminalSocket.value!.send(
|
||||||
|
|
Loading…
Reference in a new issue