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

Method writePump

server/handlers/websocket.go:167–197  ·  view source on GitHub ↗

writePump writes messages to the WebSocket connection

(wsConn *WebSocketConnection)

Source from the content-addressed store, hash-verified

165
166// writePump writes messages to the WebSocket connection
167func (h *WebSocketHandler) writePump(wsConn *WebSocketConnection) {
168 ticker := time.NewTicker(54 * time.Second)
169 defer func() {
170 ticker.Stop()
171 _ = wsConn.Conn.Close()
172 }()
173
174 for {
175 select {
176 case message, ok := <-wsConn.Send:
177 _ = wsConn.Conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
178 if !ok {
179 _ = wsConn.Conn.WriteMessage(websocket.CloseMessage, []byte{})
180 return
181 }
182
183 if err := wsConn.Conn.WriteMessage(websocket.TextMessage, message); err != nil {
184 return
185 }
186
187 case <-ticker.C:
188 _ = wsConn.Conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
189 if err := wsConn.Conn.WriteMessage(websocket.PingMessage, nil); err != nil {
190 return
191 }
192
193 case <-wsConn.ctx.Done():
194 return
195 }
196 }
197}
198
199// handleMessage handles different message types
200func (h *WebSocketHandler) handleMessage(wsConn *WebSocketConnection, msg *WebSocketMessage) {

Callers 1

HandleWebSocketMethod · 0.95

Calls 3

StopMethod · 0.65
CloseMethod · 0.65
AddMethod · 0.65

Tested by

no test coverage detected