readPump reads messages from the WebSocket connection
(wsConn *WebSocketConnection)
| 120 | |
| 121 | // readPump reads messages from the WebSocket connection |
| 122 | func (h *WebSocketHandler) readPump(wsConn *WebSocketConnection) { |
| 123 | defer func() { |
| 124 | h.closeConnection(wsConn) |
| 125 | }() |
| 126 | |
| 127 | _ = wsConn.Conn.SetReadDeadline(time.Now().Add(60 * time.Second)) |
| 128 | wsConn.Conn.SetPongHandler(func(string) error { |
| 129 | _ = wsConn.Conn.SetReadDeadline(time.Now().Add(60 * time.Second)) |
| 130 | return nil |
| 131 | }) |
| 132 | |
| 133 | for { |
| 134 | _, message, err := wsConn.Conn.ReadMessage() |
| 135 | if err != nil { |
| 136 | logging.Info(wsConn.ctx, "websocket.read.closed", map[string]any{ |
| 137 | "connection_id": wsConn.ID, |
| 138 | "error": err.Error(), |
| 139 | }) |
| 140 | if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { |
| 141 | logging.Error(wsConn.ctx, "websocket.read.error", map[string]any{ |
| 142 | "connection_id": wsConn.ID, |
| 143 | "error": err.Error(), |
| 144 | }) |
| 145 | } |
| 146 | break |
| 147 | } |
| 148 | |
| 149 | logging.Info(wsConn.ctx, "websocket.message.received", map[string]any{ |
| 150 | "connection_id": wsConn.ID, |
| 151 | "message": string(message), |
| 152 | }) |
| 153 | |
| 154 | // Parse message |
| 155 | var msg WebSocketMessage |
| 156 | if err := json.Unmarshal(message, &msg); err != nil { |
| 157 | h.sendError(wsConn, "invalid_message", "Failed to parse message") |
| 158 | continue |
| 159 | } |
| 160 | |
| 161 | // Handle message based on type |
| 162 | h.handleMessage(wsConn, &msg) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // writePump writes messages to the WebSocket connection |
| 167 | func (h *WebSocketHandler) writePump(wsConn *WebSocketConnection) { |
no test coverage detected