当前位置:网站首页>GoFrame缓冲输出到客户端Flush()
GoFrame缓冲输出到客户端Flush()
2022-08-09 16:10:00 【传说中胖子】
版本:v2.x
官方文档中介绍任何时候可以通过OutputBuffer方法输出缓冲区数据到客户端,并清空缓冲区数据。但使用r.Response.Flush()发现并不能实时输出。
修改下net\ghttp\ghttp_response_writer.go文件的Flush方法
if w.buffer.Len() > 0 {
w.writer.Write(w.buffer.Bytes())
w.writer.(http.Flusher).Flush() //添加此行
w.buffer.Reset()
}
以下为使用net/http原生写法
package main
import (
"fmt"
"log"
"net/http"
"time"
)
func handler(res http.ResponseWriter, r *http.Request) {
fmt.Fprintln(res, "<body>") //不加body没有效果
for i := 0; i < 10; i++ {
//fmt.Fprint(res, "<script>document.body.innerHTML = ''</script>")
fmt.Fprintf(res, "%d<br>", i)
if f, ok := res.(http.Flusher); ok {
f.Flush()
} else {
log.Println("Damn, no flush")
}
time.Sleep(1000 * time.Millisecond)
}
fmt.Fprintln(res, "</body>")
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}边栏推荐
猜你喜欢
随机推荐
如何通过 open-local 玩转容器本地存储? | 龙蜥技术
生产者-消费者线程模型学习
央企施工企业数字化转型的灵魂是什么
Now, how to choose a stage rental LED display?
<IDEA using tricks & & combined operation of common keys>
<IDEA 使用小技巧&&常用键联合操作>
Fees and inquiry methods of futures account opening exchanges
A49 - ESP8266建立AP传输XPT2046AD数据WIFI模块
什么是硬件集成开发?硬件集成开发的核心有哪些?
ABP 6.0.0-rc.1的新特性
【.NET 6】开发minimal api以及依赖注入的实现和代码演示
如何让button中的内容分两行显示
Reasons for slow startup of IDEA (1)
Arrow parquet 之 String Reader
Account opening requirements and exemptions for special futures such as crude oil
基于ABP和Magicodes实现Excel导出操作
[ Kitex 源码解读 ] 请求重试
冷冻电镜聚类中心(2D Class)粒子图像的解析
MySQL 5.5 series installation steps tutorial (graphical version)
微信开发者工具报错,提示 未找到入口 app.json 文件








