All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 1m29s
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/quic-go/webtransport-go"
|
|
)
|
|
|
|
func main() {
|
|
wm := &webtransport.Server{
|
|
CheckOrigin: func(r *http.Request) bool { return true },
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/chat", func(w http.ResponseWriter, r *http.Request) {
|
|
session, err := wm.Upgrade(w, r)
|
|
if err != nil {
|
|
fmt.Printf("Ошибка апгрейда: %v\n", err)
|
|
return
|
|
}
|
|
go handleChatSession(session)
|
|
})
|
|
|
|
fmt.Println("Сервер чата запущен на :8080")
|
|
// Traefik пробросит сюда HTTP/3 запрос
|
|
http.ListenAndServe(":8080", mux)
|
|
}
|
|
|
|
func handleChatSession(session *webtransport.Session) {
|
|
for {
|
|
// Принимаем двунаправленный поток (для сообщений)
|
|
stream, err := session.AcceptStream(context.Background())
|
|
if err != nil {
|
|
return
|
|
}
|
|
go func(s webtransport.Stream) {
|
|
defer s.Close()
|
|
buf := make([]byte, 1024)
|
|
n, _ := s.Read(buf)
|
|
fmt.Printf("Сообщение: %s\n", string(buf[:n]))
|
|
s.Write([]byte("Сервер: Принято!"))
|
|
}(*stream)
|
|
}
|
|
}
|