2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 06:47:20 +08:00

feat: 终端支持ping

This commit is contained in:
2026-01-26 20:09:33 +08:00
parent 56c1dcbd44
commit 36f2bdaf81
3 changed files with 87 additions and 2 deletions

View File

@@ -22,6 +22,11 @@ type MessageResize struct {
Rows uint `json:"rows"`
}
// MessagePing ping 消息
type MessagePing struct {
Ping bool `json:"ping"`
}
// Turn PTY 终端
type Turn struct {
ctx context.Context
@@ -78,6 +83,7 @@ func (t *Turn) Close() {
// Handle 从 WebSocket 读取输入写入 PTY
func (t *Turn) Handle(ctx context.Context) error {
var resize MessageResize
var ping MessagePing
go func() { _ = t.Pipe(ctx) }()
@@ -92,6 +98,12 @@ func (t *Turn) Handle(ctx context.Context) error {
return fmt.Errorf("failed to read ws message: %w", err)
}
// 判断是否是 ping 消息
if err = json.Unmarshal(data, &ping); err == nil && ping.Ping {
_ = t.ws.Write(ctx, websocket.MessageText, []byte(`{"pong":true}`))
continue
}
// 判断是否是 resize 消息
if err = json.Unmarshal(data, &resize); err == nil {
if resize.Resize && resize.Columns > 0 && resize.Rows > 0 {

View File

@@ -17,6 +17,10 @@ type MessageResize struct {
Rows int `json:"rows"`
}
type MessagePing struct {
Ping bool `json:"ping"`
}
type Turn struct {
ctx context.Context
stdin io.WriteCloser
@@ -72,6 +76,7 @@ func (t *Turn) Close() {
func (t *Turn) Handle(ctx context.Context) error {
var resize MessageResize
var ping MessagePing
for {
select {
@@ -84,6 +89,12 @@ func (t *Turn) Handle(ctx context.Context) error {
return fmt.Errorf("reading ws message err: %v", err)
}
// 判断是否是 ping 消息
if err = json.Unmarshal(data, &ping); err == nil && ping.Ping {
_ = t.ws.Write(ctx, websocket.MessageText, []byte(`{"pong":true}`))
continue
}
// 判断是否是 resize 消息
if err = json.Unmarshal(data, &resize); err == nil {
if resize.Resize && resize.Columns > 0 && resize.Rows > 0 {