v0.0.17.3 Переработан websocket, добавлена обработка ping/pong
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
db667f224b
commit
2145affdac
@ -38,6 +38,8 @@ func NewChatHandler(chatService service.ChatService, hub *ws.Hub, tokenAuth *aut
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *ChatHandler) HandleWebSocket(w http.ResponseWriter, r *http.Request) {
|
func (h *ChatHandler) HandleWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Printf("Incoming WebSocket headers: %+v", r.Header)
|
||||||
|
log.Printf("Cookies: %+v", r.Cookies())
|
||||||
requestedProtocol := r.Header.Get("Sec-WebSocket-Protocol")
|
requestedProtocol := r.Header.Get("Sec-WebSocket-Protocol")
|
||||||
if requestedProtocol != "" && requestedProtocol != "graphql-transport-ws" {
|
if requestedProtocol != "" && requestedProtocol != "graphql-transport-ws" {
|
||||||
http.Error(w, "Unsupported WebSocket protocol", http.StatusBadRequest)
|
http.Error(w, "Unsupported WebSocket protocol", http.StatusBadRequest)
|
||||||
|
|||||||
@ -8,12 +8,17 @@ import (
|
|||||||
func CORS(allowedOrigins []string) func(http.Handler) http.Handler {
|
func CORS(allowedOrigins []string) func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
origin := r.Header.Get("Origin")
|
// Особые правила для WebSocket
|
||||||
if r.Header.Get("Upgrade") == "websocket" {
|
if r.Header.Get("Upgrade") == "websocket" {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "*")
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Проверяем, разрешен ли источник
|
|
||||||
|
// Стандартная CORS логика для других запросов
|
||||||
|
origin := r.Header.Get("Origin")
|
||||||
if isOriginAllowed(origin, allowedOrigins) {
|
if isOriginAllowed(origin, allowedOrigins) {
|
||||||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||||
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||||
@ -23,7 +28,6 @@ func CORS(allowedOrigins []string) func(http.Handler) http.Handler {
|
|||||||
"GET, POST, PUT, DELETE, OPTIONS")
|
"GET, POST, PUT, DELETE, OPTIONS")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Обрабатываем предварительные OPTIONS-запросы
|
|
||||||
if r.Method == "OPTIONS" {
|
if r.Method == "OPTIONS" {
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
return
|
return
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user