当前位置:网站首页>Block transmission by golang gin framework
Block transmission by golang gin framework
2022-06-28 07:19:00 【nangonghen】
1) summary : Block transmission
This is a kind of " break up the whole into parts " or " Divide and conquer " The idea of , stay HTTP The expression in the agreement is "chunked" Block transfer coding . stay http Header field in response message “Transfer-Encoding: chunked”, Represents the body It is not sent at one time , It's a lot of pieces (chunk) Send... One by one , Until it's sent .
2) Coding rules for block transmission
1) Each block consists of two parts ,< Length head > and < Data blocks >;
2)< Length head > In order to CRLF( Carriage returns , namely \r\n) A line of clear text at the end , use 16 A decimal number means length ;
3)< Data blocks > Keep up with the < Length head > after , In the end CRLF ending , But the data doesn't contain CRLF;
4) Finally, we use a length of 0 The block of indicates the end of data transmission , namely “0\r\n\r\n”.
3)golang Code
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()
// Add a block transfer header field to the response header Transfer-Encoding: chunked
header.Set("Transfer-Encoding", "chunked")
header.Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
//Flush() Method , For example, the server writes data to a file , The browser will see that the contents of this file are increasing .
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) Browser Test
You can see that the browser interface gradually receives 0,1,2,3….
explain : From the browser , Not a complete message , Because it is processed by the browser , With the block removed < Length head > Information .


边栏推荐
- pytorch RNN 学习笔记
- HTTP Caching Protocol practice
- 面经---测试工程师web端自动化---大厂面试题
- 剑指offer II 091.粉刷房子
- Compilation principles final review
- 金山云团队分享 | 5000字读懂Presto如何与Alluxio搭配
- 力扣515.在每棵树行中找最大值
- Mysql57 zip file installation
- 2021 VDC: technological architecture evolution of vivo Internet service for 100 million users | PPT download attached
- 东方财富上开户是安全的吗
猜你喜欢

一个小工具可以更快的写爬虫

kubelet垃圾(退出的容器和未使用的镜像)回收源码分析

Cloud native (to be updated)

Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known

Libuv框架echo-server.c源码详解(TCP部分)

What is a consistent hash? What scenarios can it be applied to?

redis的入门学习到起飞,就这一篇搞定

Last 29 days

Kubernetes cluster lossless upgrade practice

open62541直接导入NodeSet文件
随机推荐
A small code editor can also run programs -- a summary of sublime Text3 running programs in various languages
【Rust日报】 2020-04-23 Rust 1.43.0 发布
R 语言 Hitters 数据分析
Mise en œuvre de l'actionneur asynchrone d'exécution à partir de zéro
R语言绘制 ggplot2 季节性图
XML序列化向后兼容
What is a consistent hash? What scenarios can it be applied to?
一个小工具可以更快的写爬虫
网传互联网公司加班表,排名第一的没悬念
R 语言 ggmap 可视化集群
阿里云服务器创建快照、回滚磁盘
什么是一致性哈希?可以应用在哪些场景?
金山云团队分享 | 5000字读懂Presto如何与Alluxio搭配
Code submission specification
R 和 rgl 绘制 3D 结
实现这个 issue 得700块钱人民币,有人做嘛?
Kubernetes cluster lossless upgrade practice
Mysql8.0和Mysql5.0访问jdbc连接
[c #] [reprint]furion frame address and tutorial address
Libuv框架echo-server.c源码详解(TCP部分)