我將一個客戶端(或幾個客戶端)連接到 API Gateway 中的 websockets 端點。然后,我嘗試使用以下指南將消息發回客戶端:https ://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-how-to-call-websocket-api-connections.html// Send sends a message to a connection ID.func Send(domain, stage, connectionID, message string) (events.APIGatewayProxyResponse, error) { session := session.Must(session.NewSession()) endpoint := fmt.Sprintf("https://%s/%s/@connections/%s", domain, stage, connectionID) apiClient := apigatewaymanagementapi.New(session, aws.NewConfig().WithEndpoint(endpoint)) connectionInput := apigatewaymanagementapi.PostToConnectionInput{ ConnectionId: aws.String(connectionID), Data: []byte(message), } _, err := apiClient.PostToConnection(&connectionInput) if err != nil { fmt.Println(err.Error()) return events.APIGatewayProxyResponse{StatusCode: 500}, err } return events.APIGatewayProxyResponse{StatusCode: 200}, nil}無論我是在本地調用 Send 函數還是客戶端發送消息,API Gateway 都會調用我的publishLambda,我在其中循環連接并為每個連接調用 Send,這并不重要。結果總是一樣的。NotFoundException: status code: 404, request id: 7bb1546a-c2a7-4e98-92a0-fcc7ae175d7c我嘗試過的事情:逃脫@connections和實際connectionID確??蛻舳诉B接沒有超時確保我的環境變量中有正確的 AWS 憑證確保我的 Lambda 有權調用 API Gateway確保端點格式正確:https://{api-id}.execute-api.{region}.amazonaws.com/{stage}/@connections/{connection_id}如何成功向客戶端發送消息?
AWS API Gateway WebSockets [POST]@connections 返回
慕的地8271018
2022-07-11 15:24:00