Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(guacamole): 优化GuacamoleApi的WebSocket处理 #463

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions server/api/guacamole.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"next-terminal/server/common/nt"
"path"
"strconv"
"time"

"next-terminal/server/config"
"next-terminal/server/global/session"
Expand Down Expand Up @@ -51,6 +52,8 @@ func (api GuacamoleApi) Guacamole(c echo.Context) error {
log.Warn("升级为WebSocket协议失败", log.NamedError("err", err))
return err
}
defer ws.Close()

ctx := context.TODO()
width := c.QueryParam("width")
height := c.QueryParam("height")
Expand Down Expand Up @@ -167,7 +170,24 @@ func (api GuacamoleApi) Guacamole(c echo.Context) error {
guacamoleHandler.Start()
defer guacamoleHandler.Stop()

// WebSocket Ping/Pong handler to keep connection alive
go func() {
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := ws.WriteMessage(websocket.PingMessage, nil); err != nil {
log.Debug("WebSocket Ping failed", log.String("sessionId", sessionId), log.NamedError("err", err))
_ = ws.Close()
return
}
}
}
}()

for {
ws.SetReadDeadline(time.Now().Add(60 * time.Second))
_, message, err := ws.ReadMessage()
if err != nil {
log.Debug("WebSocket已关闭", log.String("sessionId", sessionId), log.NamedError("err", err))
Expand Down