-
-
Save calvin2021y/85799c078ed133878e64580861e143d2 to your computer and use it in GitHub Desktop.
golang h2c http2 client and server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"golang.org/x/net/http2" | |
"golang.org/x/net/http2/h2c" | |
"crypto/tls" | |
"net" | |
) | |
func client() { | |
fmt.Println("s1") | |
client := &http.Client{} | |
client.Transport = &http2.Transport{ | |
AllowHTTP: true, | |
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) { | |
return net.Dial(netw, addr) | |
}} | |
resp, err := client.Get("http://localhost:8080/anc") | |
fmt.Println(err) | |
fmt.Println(resp) | |
} | |
func main() { | |
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, "Hello world123") | |
}) | |
h2s := &http2.Server{ | |
// ... | |
} | |
h1s := &http.Server{ | |
Addr: ":8080", | |
Handler: h2c.NewHandler(handler, h2s), | |
} | |
go h1s.ListenAndServe() | |
client() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment