madipo2611 a5a90bed7e v0.0.1
2025-04-28 15:14:02 +03:00

80 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package handlers
import (
"net/http"
"strconv"
"tailly_back_v2/internal/domain"
"tailly_back_v2/internal/service"
"tailly_back_v2/internal/ws"
"github.com/go-chi/chi/v5"
"github.com/gorilla/websocket"
)
type ChatHandler struct {
chatService service.ChatService
hub *ws.ChatHub
}
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true // В production заменить на проверку origin
},
}
func (h *ChatHandler) WebSocketConnection(w http.ResponseWriter, r *http.Request) {
userID, err := strconv.Atoi(chi.URLParam(r, "userID"))
if err != nil {
http.Error(w, "Invalid user ID", http.StatusBadRequest)
return
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
http.Error(w, "Could not open websocket connection", http.StatusBadRequest)
return
}
client := &ws.Client{
UserID: userID,
Send: make(chan *domain.Message, 256),
}
h.hub.Register(client)
// Горутина для чтения сообщений
go h.readPump(conn, client)
// Горутина для записи сообщений
go h.writePump(conn, client)
}
func (h *ChatHandler) readPump(conn *websocket.Conn, client *ws.Client) {
defer func() {
h.hub.Unregister(client)
conn.Close()
}()
for {
_, message, err := conn.ReadMessage()
if err != nil {
break
}
// Обработка входящих сообщений (если нужно)
}
}
func (h *ChatHandler) writePump(conn *websocket.Conn, client *ws.Client) {
defer conn.Close()
for {
message, ok := <-client.Send
if !ok {
conn.WriteMessage(websocket.CloseMessage, []byte{})
return
}
if err := conn.WriteJSON(message); err != nil {
return
}
}
}