From 5ac43df821ce73af0037b9c1fdc945655c8156b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Fri, 25 Oct 2024 22:31:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B0=9D=E8=AF=95=E6=94=B9=E5=96=84?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/service/ws.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/service/ws.go b/internal/service/ws.go index da707becf0..9c10f103ea 100644 --- a/internal/service/ws.go +++ b/internal/service/ws.go @@ -5,7 +5,9 @@ import ( "context" "github.com/TheTNB/panel/internal/http/request" "net/http" + "strings" "sync" + "time" "github.com/TheTNB/panel/internal/app" "github.com/TheTNB/panel/internal/biz" @@ -100,10 +102,28 @@ func (s *WsService) Exec(w http.ResponseWriter, r *http.Request) { go func() { scanner := bufio.NewScanner(out) + var batch []string + ticker := time.NewTicker(200 * time.Millisecond) + defer ticker.Stop() + + go func() { + for range ticker.C { + if len(batch) > 0 { + _ = ws.WriteMessage(websocket.TextMessage, []byte(strings.Join(batch, "\n"))) + batch = nil + } + } + }() + for scanner.Scan() { line := scanner.Text() - _ = ws.WriteMessage(websocket.TextMessage, []byte(line)) + batch = append(batch, line) + } + + if len(batch) > 0 { + _ = ws.WriteMessage(websocket.TextMessage, []byte(strings.Join(batch, "\n"))) } + if err = scanner.Err(); err != nil { _ = ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "failed to read command output")) }