- server push作用的理解
- 客户端和服务端建立连接之后,服务器主动将一些资源推送给服务端。
- 注意:推送的内容,要放在发送响应数据之前。
- server push示意图
- server push特点
- 理论上,缩短数据加载的时间
- 将请求数据存放在缓存中
- server push是http2的产物
- http2是强制性使用TLS(传送安全层)的,也就是https请求。因此为了完成代码,我们需要一个密钥(key)和一个证书(crt)。产生证书的方法。在Linux中运行以下命令(在当前路径下生成):
openssl req -newkey rsa:2048 -nodes -keyout server.key -x509 -days 365 -out server.crt
- 实现代码
- 参考代码
package main import( "os" "fmt" "log" "net/http" "io/ioutil" "encoding/base64" ) var base64img string var image []byte func init(){ var err error image,err = ioutil.ReadFile("./7775.jpg") if err!=nil{ fmt.Println("初始化读取文件错误") } base64img = base64.StdEncoding.EncodeToString(image) fmt.Println(base64img) } func main(){ fmt.Println("server push") http.Handle("/",http.StripPrefix("/",http.FileServer(http.Dir("./")))) //建立静态文件服务器(当前文件夹下有,app.js、style.css、7775.jpg文件) http.HandleFunc("/serverPush",serverPush) http.HandleFunc("/image",handleImage) err:=http.ListenAndServeTLS(":8080","server.crt","server.key",nil) //使用tls的方式监听端口,这里是访问是使用https进行访问 if err!=nil{ log.Fatal(err) } } func serverPush(w http.ResponseWriter,r *http.Request){ path,err:=os.Getwd() if err!=nil{ panic(err) } fmt.Println("paht=",path) pusher,ok := w.(http.Pusher) //使用断言的方法,判断时候只是server push。 fmt.Println("pusher:",pusher,ok) if ok { fmt.Println("支持serverPush") // push函数:可以发送文件、api接口 if err:= pusher.Push("/image",nil);err!=nil{ log.Fatal("Failed to push:",err) } if err:= pusher.Push("/app.js",nil);err!=nil{ log.Fatal("Failed to push:",err) } if err:= pusher.Push("/style.css",nil);err!=nil{ log.Fatal("Failed to push:",err) } } w.Header().Add("Content-Type","text/html") fmt.Fprintf(w,`<html> <head> <title>Hello World</title> <script src="/app.js"></script> <link rel="stylesheet" href="/style.css""> </head> <body> Hello, gopher! <img src="/image"> </body> </html>`) } func handleImage(w http.ResponseWriter,r *http.Request){ w.Header().Set("Content-Type","image/png") w.Write(image) }
- 运行之后,chrome浏览器输入:https://localhost:8080/
来源:博客园
作者:Myuniverse
链接:https://www.cnblogs.com/MyUniverse/p/11618897.html