【发布时间】:2021-02-10 04:54:22
【问题描述】:
我将一个客户端(或几个客户端)连接到 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 调用我的 publish Lambda 都没有关系,我在其中循环连接并为每个连接调用 Send。
结果总是一样的。
NotFoundException:
status code: 404, request id: 7bb1546a-c2a7-4e98-92a0-fcc7ae175d7c
我尝试过的事情:
- 转义的
@connections和实际的connectionID - 确保客户端连接没有超时
- 确保我的环境变量中有正确的 AWS 凭证
- 确保我的 Lambda 有权调用 API Gateway
- 确保端点格式正确:
https://{api-id}.execute-api.{region}.amazonaws.com/{stage}/@connections/{connection_id}
我怎样才能成功地向客户端发送消息?
【问题讨论】:
标签: go websocket connection http-status-code-404 aws-api-gateway