当前位置:网站首页>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))
}
边栏推荐
- 期货开户流程和手续费如何调整
- PGSQL backup tool, which is better?
- 国星光电吉利产业园项目主体结构全面封顶,将重点生产 RGB 小间距、Mini LED、TOP LED 等产品
- 如何仿造一个websocket请求?
- ceph2
- 2.1, pay attention to the network based on parallel context scenario text image super-resolution
- 微信开发者工具程序开发好后,不报错,但是黑屏「建议收藏」
- <IDEA using tricks & & combined operation of common keys>
- A49 - ESP8266建立AP传输XPT2046AD数据WIFI模块
- 期货开户交易所的手续费和查询方法
猜你喜欢
随机推荐
SQL trill interview: send you a universal template, to?(key, each user to log on to the maximum number of consecutive monthly)
生产者-消费者线程模型学习
qiucode.cn网站之文章详情实现代码块可点击按钮进行复制
3 Feature Binning Methods!
110+ public professional datasets summarized
MySQL索引的B+树到底有多高?
Use SourceTree to add SSH public key and clone the code cloud project (note finishing)
What is hardware integrated development?What are the cores of hardware integrated development?
.NET 6 study notes (4) - Solve the Nullable warning in VS2022
WPF 实现柱形统计图
WinForm(四)一种实现登录的方式
MySQL 5.5 series installation steps tutorial (graphical version)
【开源教程4】疯壳·开源编队无人机-OPENMV 脚本烧写
In-depth understanding of MySQL common data types and data type selection optimization
使用SourceTree添加SSH公钥并克隆码云项目(笔记整理篇)
A40 - 基于51单片机的GSM模块优化设计
微服务:事务管理
3种特征分箱方法!
pgsql备份工具,哪个比较好?
Arrow parquet 之 String Reader