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

Method sendMessage

server/handlers/websocket.go:437–477  ·  view source on GitHub ↗

sendMessage sends a message to the WebSocket client

(wsConn *WebSocketConnection, msgType string, payload map[string]any)

Source from the content-addressed store, hash-verified

435
436// sendMessage sends a message to the WebSocket client
437func (h *WebSocketHandler) sendMessage(wsConn *WebSocketConnection, msgType string, payload map[string]any) {
438 // 检查 context 是否已取消
439 if wsConn.ctx.Err() != nil {
440 return
441 }
442
443 msg := WebSocketMessage{
444 Type: msgType,
445 Payload: payload,
446 }
447
448 data, err := json.Marshal(msg)
449 if err != nil {
450 logging.Error(wsConn.ctx, "websocket.marshal.error", map[string]any{
451 "error": err.Error(),
452 })
453 return
454 }
455
456 // 使用 defer recover 防止发送到已关闭的 channel 导致 panic
457 defer func() {
458 if r := recover(); r != nil {
459 logging.Warn(wsConn.ctx, "websocket.send.recovered", map[string]any{
460 "connection_id": wsConn.ID,
461 "message_type": msgType,
462 "panic": r,
463 })
464 }
465 }()
466
467 select {
468 case wsConn.Send <- data:
469 case <-wsConn.ctx.Done():
470 default:
471 // Channel full, skip message
472 logging.Warn(wsConn.ctx, "websocket.send.dropped", map[string]any{
473 "connection_id": wsConn.ID,
474 "message_type": msgType,
475 })
476 }
477}
478
479// sendError sends an error message to the WebSocket client
480func (h *WebSocketHandler) sendError(wsConn *WebSocketConnection, code, message string) {

Callers 7

handleMessageMethod · 0.95
handleChatMethod · 0.95
sendErrorMethod · 0.95
handleTodoListRequestMethod · 0.95
broadcastToAllMethod · 0.95
sendApprovalRequestMethod · 0.45

Calls 3

ErrorFunction · 0.92
WarnFunction · 0.92
ErrorMethod · 0.65

Tested by

no test coverage detected