package main
import (
"github.com/lucas-clemente/quic-go"
"github.com/lucas-clemente/quic-go/http3"
"log"
"net/http"
)
const KPORT = ":1441"
const KCERT = "./example.com+5.pem"
const KKEY = "./example.com+5-key.pem"
const KHTTP3 = true
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(
`<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
hello
</body>
</html>
`,
))
})
srv := &http.Server{
Addr: KPORT,
Handler: mux,
}
if !KHTTP3 {
log.Fatal(srv.ListenAndServeTLS(
KCERT,
KKEY,
))
} else {
qconf := quic.Config{}
// https://github.com/lucas-clemente/quic-go/blob/master/example/main.go#L187
quicServer := http3.Server{
Server: srv,
QuicConfig: &qconf,
}
log.Fatal(quicServer.ListenAndServeTLS(
KCERT,
KKEY,
))
}
}
KHTTP3 切换上面的 HTTP3 quic 包quic-go
。当我启用 KHTTP3 并尝试连接时,浏览器的行为就像我没有启动任何 Go 服务器一样。任何地方都没有错误消息。我的代码有什么问题?我试图坚持https://github.com/lucas-clemente/quic-go/blob/master/example/main.go#L187上的示例
编辑:我的新代码运行良好,由 mh-cbon 建议是:
log.Fatal(http3.ListenAndServe(
KPORT,
KCERT,
KKEY,
mux,
))