Skip to content

langhuihui/WebTransport-Go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebTransportServer

基于quic-go的封装,用于支持WebTransport。以下是使用Example

import 	(
	"fmt"
	"log"
	"net/http"
  . "github.com/langhuihui/webtransport-go"
)
func main(){
  http.HandleFunc("/counter", func(rw http.ResponseWriter, r *http.Request) {
    fmt.Printf("%v", r.Header)
  })
  server := NewWebTransportServer(Config{
    ListenAddr:     ":4433",
    TLSCertPath:    "quic.pem",
    TLSKeyPath:     "quic.key",
    AllowedOrigins: []string{"localhost", "::1"},
  })
  go func() {
    for sess := range server.Session {
      fmt.Printf("ReceiveSession:%v", sess.RemoteAddr())
      go func() {
        for {
          msg, _ := sess.ReceiveMessage()
          fmt.Printf("ReceiveMessage:% x", msg)
        }
      }()
      go func() {
        for {
          uniS, _ := sess.AcceptUniStream(sess.Context())
          buf := make([]byte, 1024)
          n, _ := uniS.Read(buf)
          fmt.Printf("receive from UniStream:% x", buf[:n])
        }
      }()
      go func() {
        for {
          s, _ := sess.AcceptStream(sess.Context())
          buf := make([]byte, 1024)
          n, _ := s.Read(buf)
          fmt.Printf("receive from Stream:% x", buf[:n])
          s.Write(buf[:n])
        }
      }()
    }
  }()
  ctx, cancel := context.WithCancel(context.Background())
  if err := server.Run(ctx); err != nil {
    log.Fatal(err)
    cancel()
  }
}

About

WebTransport Server based on quic-go

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages