Skip to content

Instantly share code, notes, and snippets.

@fwoodruff
Last active November 25, 2023 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fwoodruff/d704d1650cd4a596c3bb9392f1087281 to your computer and use it in GitHub Desktop.
Save fwoodruff/d704d1650cd4a596c3bb9392f1087281 to your computer and use it in GitHub Desktop.
QUIC Client
package main
import (
"bytes"
"context"
"crypto/tls"
"log"
"net/http"
"time"
"github.com/quic-go/quic-go"
)
func main() {
tlsConfig := &tls.Config{
InsecureSkipVerify: true, // testing only
NextProtos: []string{"h3", "http/1.1"},
}
url := "localhost:8080"
req, _ := http.NewRequest("GET", url, nil)
var buf bytes.Buffer
req.Write(&buf)
requestBytes := buf.Bytes()
ctx := context.Background()
connection, err := quic.DialAddr(ctx, url, tlsConfig, nil)
if err != nil {
println(err.Error())
return
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
stream, err := connection.OpenUniStreamSync(ctx)
if err != nil {
log.Fatal(err)
return
}
n, err := stream.Write(requestBytes)
if err != nil {
log.Fatal(err)
}
if err = stream.Close(); err != nil {
log.Fatal(err)
}
println("ok")
println(n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment