当前位置:网站首页>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…。
说明:从浏览器中看见的,不是完整的报文,因为是经过浏览器处理的,去掉了块的<长度头>信息。


边栏推荐
- 【Rust翻譯】從頭實現Rust异步執行器
- 在idea中,get和set方法爆红可能是没有安装Lombok插件
- Hungry for a long time, the brain will enter the "power saving mode"! Your senses are weakened and you can't see clearly
- Will Internet talents be scarce in the future? Which technology directions are popular?
- Puge -- singleton mode
- Compilation principles final review
- R 和 rgl 绘制 3D 结
- My MVVM open source project "travel epidemic prevention app" has been released
- Comment la passerelle BACnet / IP recueille - t - elle les données du système central de contrôle des bâtiments?
- 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
猜你喜欢

MySQL installation steps - how to create a virtual machine under Linux (1)

Comprehensive analysis of real enterprise software testing process

Principle and practice of bytecode reference detection

pytorch RNN 学习笔记

阿里云服务器创建快照、回滚磁盘

Evolution of vivo push platform architecture

Niubi 666, this project makes web page making as simple as building blocks

linux下修改mysql端口号

What should I do if the version is incompatible with the jar package conflict?

C language tutorial
随机推荐
LeetCode+ 51 - 55 回溯、动态规划专题
C language tutorial
R 语言绘制 动画气泡图
服务器正文18:UDP可靠传输的理解和思考(读云凤博客有感)
Mise en œuvre de l'actionneur asynchrone d'exécution à partir de zéro
文件头信息对照表
Hungry for a long time, the brain will enter the "power saving mode"! Your senses are weakened and you can't see clearly
linux下修改mysql端口号
强化学习——格子世界
Design and practice of vivo sensitive word matching system
小小一款代码编辑器竟然也可以有程序运行之功能——Sublime Text3运行各种语言程序的总结
网传互联网公司加班表,排名第一的没悬念
Extern "C" overview
未来互联网人才还稀缺吗?哪些技术方向热门?
The practice of traffic and data isolation in vivo Reviews
Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known
MySQL master-slave replication, detailed configuration, create unable to connect processing prompt
Can okcc call centers work without computers?
R 语言 Hitters 数据分析
金山云团队分享 | 5000字读懂Presto如何与Alluxio搭配