当前位置:网站首页>golang gin框架进行分块传输
golang gin框架进行分块传输
2022-06-28 07:15:00 【nangonghen】
1)概述:分块传输
这是一种"化整为零"或"分治"的思路,在 HTTP 协议里的体现就是"chunked"分块传输编码。在http响应报文中用头字段“Transfer-Encoding: chunked”,表示响应中的body不是一次性发送完毕,而是分成了许多的块(chunk)逐个发送,直到发送完毕。
2)分块传输的编码规则
1)每个分块包含两个部分,<长度头>和<数据块>;
2)<长度头>是以 CRLF(回车换行,即\r\n)结尾的一行明文,用 16 进制数字表示长度;
3)<数据块>紧跟在<长度头>后,最后也用 CRLF 结尾,但数据不包含 CRLF;
4)最后用一个长度为 0 的块表示数据传输结束,即“0\r\n\r\n”。
3)golang代码
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func main() {
server := &APIServer{
engine: gin.Default(),
}
server.registryApi()
server.engine.Run(":38080")
}
type APIServer struct {
engine *gin.Engine
}
func (s *APIServer) registryApi() {
registryStream(s.engine)
}
func registryStream(engine *gin.Engine) {
engine.GET("/stream", func(ctx *gin.Context) {
w := ctx.Writer
header := w.Header()
//在响应头添加分块传输的头字段Transfer-Encoding: chunked
header.Set("Transfer-Encoding", "chunked")
header.Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
//Flush()方法,好比服务端在往一个文件中写了数据,浏览器会看见此文件的内容在不断地增加。
w.Write([]byte(`
<html>
<body>
`))
w.(http.Flusher).Flush()
for i:=0 ;i<10; i++{
w.Write([]byte(fmt.Sprintf(`
<h1>%d</h1>
`,i)))
w.(http.Flusher).Flush()
time.Sleep(time.Duration(1) * time.Second)
}
w.Write([]byte(`
</body>
</html>
`))
w.(http.Flusher).Flush()
})
}
4)浏览器测试
可以发现浏览器界面逐渐收到0,1,2,3…。
说明:从浏览器中看见的,不是完整的报文,因为是经过浏览器处理的,去掉了块的<长度头>信息。
边栏推荐
- R 语言绘制 动画气泡图
- redis的入门学习到起飞,就这一篇搞定
- 小小一款代码编辑器竟然也可以有程序运行之功能——Sublime Text3运行各种语言程序的总结
- Recommend several 0 code, free, learning and using visualization tools
- [C language] detailed explanation of C language to obtain array length
- R language ggmap
- 什么是一致性哈希?可以应用在哪些场景?
- 编译配置in文件
- Optimization steps of SQL statements (II) -- MySQL statement optimization
- mysql57 zip文件安装
猜你喜欢
云原生(待更新)
What is a consistent hash? What scenarios can it be applied to?
pytorch RNN 学习笔记
HTTP Caching Protocol practice
mysql57 zip文件安装
A gadget can write crawlers faster
Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known
Recommend 10 popular jupyter notebook plug-ins to make you fly efficiently
什么是一致性哈希?可以应用在哪些场景?
linux下修改mysql端口号
随机推荐
mysql57 zip文件安装
My MVVM open source project "travel epidemic prevention app" has been released
VM332 WAService. js:2 Error: _ vm. Changetabs is not a function
Drawing animated bubble chart with R language
Recommend 10 popular jupyter notebook plug-ins to make you fly efficiently
R language Kolmogorov Smirnov tests whether the two samples follow the same distribution.
R language ggmap visual cluster
7-2 芬兰木棋 结构体排序
Evolution of vivo push platform architecture
Huawei cloud computing physical node cna installation tutorial
Puge -- singleton mode
小小一款代码编辑器竟然也可以有程序运行之功能——Sublime Text3运行各种语言程序的总结
Can okcc call centers work without computers?
[C#][转载]furion框架地址和教程地址
hack the box:RouterSpace题解
An important term in MySQL -- CRUD
自律挑战30天
Pfizer's new Guankou medicine has entered the Chinese market, and the listing of relevant products of domestic pharmaceutical enterprises is just around the corner
服务器正文18:UDP可靠传输的理解和思考(读云凤博客有感)
Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known