new quic release [chat tests] 5
All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 4m1s
All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 4m1s
This commit is contained in:
39
main.go
39
main.go
@@ -3,44 +3,63 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/quic-go/webtransport-go"
|
"github.com/quic-go/webtransport-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
wm := &webtransport.Server{
|
wm := &webtransport.Server{
|
||||||
CheckOrigin: func(r *http.Request) bool { return true },
|
CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
}
|
}
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
|
||||||
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 := wm.Upgrade(w, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Ошибка апгрейда: %v\n", err)
|
log.Println("upgrade error:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go handleChatSession(session)
|
go handleChatSession(session)
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println("Сервер чата запущен на :8080")
|
server := &http.Server{
|
||||||
// Traefik пробросит сюда HTTP/3 запрос
|
Addr: ":8080",
|
||||||
http.ListenAndServe(":8080", mux)
|
Handler: mux,
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Server started")
|
||||||
|
|
||||||
|
log.Fatal(server.ListenAndServe())
|
||||||
}
|
}
|
||||||
|
|
||||||
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.Printf("Сообщение: %s\n", string(buf[:n]))
|
|
||||||
s.Write([]byte("Сервер: Принято!"))
|
fmt.Println("Message:", string(buf[:n]))
|
||||||
}(*stream)
|
|
||||||
|
s.Write([]byte("Server: OK"))
|
||||||
|
|
||||||
|
}(stream)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user