quic-go/webtransport-go

give a echo example in repo

Closed this issue · 2 comments

I have tried some way to run a webtransport example.,but it can not run correctly, the code is like this:


package main

import (
	"context"
	"crypto/tls"
	"crypto/x509"
	"fmt"
	"io"
	"log"
	"net/http"
	"time"

	"github.com/quic-go/quic-go"
	"github.com/quic-go/quic-go/http3"
	"github.com/quic-go/webtransport-go"

	"transport-go/testdata"
)

func RunServer() {
	// create a new webtransport.Server, listening on (UDP) port 443
	s := webtransport.Server{
		H3: http3.Server{
			QuicConfig: &quic.Config{Allow0RTT: true},
			// TLSConfig:  &tls.Config{InsecureSkipVerify: true}
		},
	}

	// Create a new HTTP endpoint /webtransport.
	http.HandleFunc("/webtransport", func(w http.ResponseWriter, r *http.Request) {
		conn, err := s.Upgrade(w, r)
		if err != nil {
			log.Printf("upgrading failed: %s", err)
			w.WriteHeader(500)
			return
		}
		fmt.Println("conn:", conn)
		// Handle the connection. Here goes the application logic.
		go func() {
			for {
				stream, err := conn.AcceptStream(context.Background())
				if err != nil {
					fmt.Println("server read err:", err)
					return
				}

				if _, err := io.Copy(stream, stream); err != nil {
					fmt.Println("server write err:", err)
				}
			}

		}()
	})

	if err := s.ListenAndServeTLS(testdata.GetCertificatePaths()); err != nil {
		log.Fatalln("listen err:", err)
	}
}

func RunClient() {

	pool, err := x509.SystemCertPool()
	if err != nil {
		log.Fatal(err)
	}
	testdata.AddRootCA(pool)

	var d = webtransport.Dialer{
		RoundTripper: &http3.RoundTripper{
			TLSClientConfig: &tls.Config{
				RootCAs:            pool,
				InsecureSkipVerify: true,
			},
		},
	}
	// ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5)
	// defer cancel()
	rsp, conn, err := d.Dial(context.Background(), "https://localhost:443", nil)
	// overUDP
	if err != nil {
		log.Fatalln("dial err:", err)
	}
	fmt.Println("conn:", conn)
	fmt.Println("rsp:", rsp)
	stream, err := conn.OpenStream()
	if err != nil {
		log.Fatalln("err:", err)
		return
	}
	go func() {
		maxData := make([]byte, 4096)
		for {
			recvMsg, err := stream.Read(maxData)
			if err != nil {
				_ = stream.Close()
				return
			}
			fmt.Println("recvMsg:", recvMsg)
		}
	}()
	write, err := stream.Write([]byte("hello"))
	if err != nil {
		fmt.Println("send err:", write)
		return
	}
	fmt.Println("send msg:", write)
}

func main() {
	go RunServer()
	time.Sleep(time.Second * 2)
	go RunClient()
	time.Sleep(time.Second * 10)
}

tls config is coped from quic-go internal testdata. The error is like this:

2023/10/31 09:47:41 dial err: received status 404

Maybe something went wrong, I can't debug the code. Could the code repository contain complete examples

It literally says what you’re doing wrong in the comment of your own code. You need to make the request to the right HTTP endpoint.

It literally says what you’re doing wrong in the comment of your own code. You need to make the request to the right HTTP endpoint.
I have checkouted the url and path.And some error also occurs

2023/10/31 18:08:58 dial err: timeout: no recent network activity
exit status 1

os: win11.
could the repo have a complete example, please.
I have sloved this problem.