MCPcopy Create free account
hub / github.com/astercloud/aster / readPump

Method readPump

server/handlers/websocket.go:122–164  ·  view source on GitHub ↗

readPump reads messages from the WebSocket connection

(wsConn *WebSocketConnection)

Source from the content-addressed store, hash-verified

120
121// readPump reads messages from the WebSocket connection
122func (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
167func (h *WebSocketHandler) writePump(wsConn *WebSocketConnection) {

Callers 1

HandleWebSocketMethod · 0.95

Calls 7

closeConnectionMethod · 0.95
sendErrorMethod · 0.95
handleMessageMethod · 0.95
InfoFunction · 0.92
ErrorFunction · 0.92
AddMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected