mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-02-24 06:44:17 +08:00
feat: 添加终端心跳包,增强连接稳定性,并为后续延迟检测做准备
This commit is contained in:
parent
77c0eb99f0
commit
6fb1e690aa
2 changed files with 32 additions and 6 deletions
|
@ -35,15 +35,17 @@ func (w *safeBuffer) Reset() {
|
|||
}
|
||||
|
||||
const (
|
||||
wsMsgCmd = "cmd"
|
||||
wsMsgResize = "resize"
|
||||
wsMsgCmd = "cmd"
|
||||
wsMsgResize = "resize"
|
||||
wsMsgHeartbeat = "heartbeat"
|
||||
)
|
||||
|
||||
type wsMsg struct {
|
||||
Type string `json:"type"`
|
||||
Data string `json:"data,omitempty"` // wsMsgCmd
|
||||
Cols int `json:"cols,omitempty"` // wsMsgResize
|
||||
Rows int `json:"rows,omitempty"` // wsMsgResize
|
||||
Type string `json:"type"`
|
||||
Data string `json:"data,omitempty"` // wsMsgCmd
|
||||
Cols int `json:"cols,omitempty"` // wsMsgResize
|
||||
Rows int `json:"rows,omitempty"` // wsMsgResize
|
||||
Timestamp int `json:"timestamp,omitempty"` // wsMsgHeartbeat
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
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 terminalSocket = ref<WebSocket>();
|
||||
const term = ref<Terminal>();
|
||||
const heartbeatTimer = ref<number>();
|
||||
|
||||
const readyWatcher = watch(
|
||||
() => webSocketReady.value && termReady.value,
|
||||
|
@ -59,6 +60,10 @@ const onWSReceive = (message: MessageEvent) => {
|
|||
}
|
||||
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) => {
|
||||
if (heartbeatTimer.value) {
|
||||
clearInterval(heartbeatTimer.value);
|
||||
}
|
||||
if (term.value) {
|
||||
term.value.write(ev.reason);
|
||||
}
|
||||
|
@ -125,6 +133,16 @@ const initTerm = () => {
|
|||
terminalSocket.value.onmessage = onWSReceive;
|
||||
terminalSocket.value.onclose = closeRealTerminal;
|
||||
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) => {
|
||||
if (isWsOpen()) {
|
||||
terminalSocket.value!.send(
|
||||
|
|
Loading…
Reference in a new issue