当前位置:网站首页>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 .


边栏推荐
猜你喜欢

看似简单的光耦电路,实际使用中应该注意些什么?

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

Devtools implementation principle and performance analysis practice

Open62541 import nodeset file directly

推荐10个好用到爆的Jupyter Notebook插件,让你效率飞起
Face to face experience --- test engineer web side automation --- interview questions for large factories

Yesterday, I went to a large factory for an interview and asked me to do four arithmetic operations. Fortunately, I am smart enough

LeetCode+ 51 - 55 回溯、动态规划专题

In idea, the get and set methods may be popular because the Lombok plug-in is not installed

kubelet垃圾(退出的容器和未使用的镜像)回收源码分析
随机推荐
推荐10个好用到爆的Jupyter Notebook插件,让你效率飞起
Hungry for a long time, the brain will enter the "power saving mode"! Your senses are weakened and you can't see clearly
Cloud native (to be updated)
云原生(待更新)
[c #] [reprint]furion frame address and tutorial address
Jetpack - defects of livedata component and Countermeasures
R 语言 ggmap 可视化集群
2021 VDC: technological architecture evolution of vivo Internet service for 100 million users | PPT download attached
XML序列化向后兼容
Force buckle 515 Find the maximum value in each tree row
BACnet/IP网关如何采集楼宇集中控制系统数据
PLC -- 笔记
File header information cross reference table
服务器正文18:UDP可靠传输的理解和思考(读云凤博客有感)
炒股开户在手机上安全吗?
Mysql8.0和Mysql5.0访问jdbc连接
编译原理期末复习
Design and practice of vivo sensitive word matching system
[C language] detailed explanation of C language to obtain array length
How bacnet/ip gateway collects data of building centralized control system