new quic release [chat tests] 6
All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 1m29s
All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 1m29s
This commit is contained in:
18
main.go
18
main.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/quic-go/quic-go/http3"
|
||||||
"github.com/quic-go/webtransport-go"
|
"github.com/quic-go/webtransport-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,13 +14,13 @@ func main() {
|
|||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
wm := &webtransport.Server{
|
wt := &webtransport.Server{
|
||||||
CheckOrigin: func(r *http.Request) bool { return true },
|
CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
}
|
}
|
||||||
|
|
||||||
mux.HandleFunc("/chat", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/chat", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
session, err := wm.Upgrade(w, r)
|
session, err := wt.Upgrade(w, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("upgrade error:", err)
|
log.Println("upgrade error:", err)
|
||||||
return
|
return
|
||||||
@@ -28,12 +29,12 @@ func main() {
|
|||||||
go handleChatSession(session)
|
go handleChatSession(session)
|
||||||
})
|
})
|
||||||
|
|
||||||
server := &http.Server{
|
server := http3.Server{
|
||||||
Addr: ":8080",
|
Addr: ":8080",
|
||||||
Handler: mux,
|
Handler: mux,
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Server started")
|
fmt.Println("HTTP/3 server started")
|
||||||
|
|
||||||
log.Fatal(server.ListenAndServe())
|
log.Fatal(server.ListenAndServe())
|
||||||
}
|
}
|
||||||
@@ -41,25 +42,20 @@ func main() {
|
|||||||
func handleChatSession(session *webtransport.Session) {
|
func handleChatSession(session *webtransport.Session) {
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
stream, err := session.AcceptStream(context.Background())
|
stream, err := session.AcceptStream(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go func(s *webtransport.Stream) {
|
go func(s webtransport.Stream) {
|
||||||
|
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
buf := make([]byte, 1024)
|
buf := make([]byte, 1024)
|
||||||
|
|
||||||
n, _ := s.Read(buf)
|
n, _ := s.Read(buf)
|
||||||
|
|
||||||
fmt.Println("Message:", string(buf[:n]))
|
fmt.Println("Message:", string(buf[:n]))
|
||||||
|
|
||||||
s.Write([]byte("Server: OK"))
|
s.Write([]byte("Server: OK"))
|
||||||
|
}(*stream)
|
||||||
}(stream)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user