From ea7a8207e53fde4c536b56378acef862a3c26a0d Mon Sep 17 00:00:00 2001 From: Sakurasan <1173092237@qq.com> Date: Thu, 25 May 2023 19:59:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96stream?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- router/router.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/router/router.go b/router/router.go index 2fc507b..43643a5 100644 --- a/router/router.go +++ b/router/router.go @@ -413,7 +413,7 @@ func HandleProy(c *gin.Context) { if resp.StatusCode == 200 && localuser { if isStream { - contentCh := fetchResponseContent(writer, reader) + contentCh := fetchResponseContent(c, reader) var buffer bytes.Buffer for content := range contentCh { buffer.WriteString(content) @@ -536,15 +536,18 @@ func HandleUsage(c *gin.Context) { c.JSON(200, usage) } -func fetchResponseContent(w *bufio.Writer, responseBody *bufio.Reader) <-chan string { +func fetchResponseContent(ctx *gin.Context, responseBody *bufio.Reader) <-chan string { contentCh := make(chan string) go func() { defer close(contentCh) for { line, err := responseBody.ReadString('\n') if err == nil { - w.WriteString(line) - w.Flush() + lines := strings.Split(line, "") + for _, word := range lines { + ctx.Writer.WriteString(word) + ctx.Writer.Flush() + } if line == "\n" { continue }