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


边栏推荐
猜你喜欢

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

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

Evolution of vivo push platform architecture

【C语言】详解 C 语言获取数组长度

ABAP skill tree

Libuv framework echo server C source code explanation (TCP part)

Techo day Tencent technology open day, June 28 online waiting for you!

Comprehensive analysis of real enterprise software testing process

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

BACnet/IP網關如何采集樓宇集中控制系統數據
随机推荐
Open62541 import nodeset file directly
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
Practice of traffic recording and playback in vivo
《微信小程序-基础篇》带你了解小程序中的生命周期(一)
ABAP 技能树
云原生(待更新)
No suspense about the No. 1 Internet company overtime table
Reading notes - MySQL technology l act: InnoDB storage engine (version 2)
小小一款代码编辑器竟然也可以有程序运行之功能——Sublime Text3运行各种语言程序的总结
MySQL installation steps - Linux configuration file JDK installation (II)
C language tutorial
Jinshan cloud team shared | 5000 words to understand how Presto matches with alluxio
Face to face experience --- test engineer web side automation --- interview questions for large factories
My MVVM open source project "travel epidemic prevention app" has been released
Comprehensive analysis of real enterprise software testing process
This keyword details
Top 25 most popular articles on vivo Internet technology in 2021
open62541直接导入NodeSet文件
DOM parsing of XML file case code sentence by sentence analysis