-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush-server.go
41 lines (36 loc) · 1.01 KB
/
push-server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"crypto/tls"
"golang.org/x/net/http2"
"log"
"net/http"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// 主动推送资源
if pusher, ok := w.(http.Pusher); ok {
// 这里可以根据具体逻辑选择推送哪些资源
if err := pusher.Push("/extra/data", nil); err != nil {
log.Printf("Failed to push: %v", err)
}
}
// 正常响应请求
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("Response to main request"))
})
mux.HandleFunc("/extra/data", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("This is pushed data!"))
})
server := &http.Server{
Addr: ":8443",
Handler: mux,
TLSConfig: &tls.Config{
NextProtos: []string{http2.NextProtoTLS},
},
}
http2.ConfigureServer(server, &http2.Server{})
log.Println("Serving on https://localhost:8443")
log.Fatal(server.ListenAndServeTLS("server.crt", "server.key"))
}