AWS API 网关 WebSockets [POST]@connections 返回 404 NotFound
AWS API Gateway WebSockets [POST]@connections Returning 404 NotFound
我将一个客户端(或几个客户端)连接到 API 网关中的 websockets 端点。
然后,我尝试 post 使用以下准则将消息返回给客户端: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 网关
- 确保端点格式正确:
https://{api-id}.execute-api.{region}.amazonaws.com/{stage}/@connections/{connection_id}
如何才能成功发送消息给客户端?
原来是这条线
endpoint := fmt.Sprintf("https://%s/%s/@connections/%s", domain, stage, connectionID)
需要变成这个
endpoint := fmt.Sprintf("https://%s/%s/", domain, stage)
我将一个客户端(或几个客户端)连接到 API 网关中的 websockets 端点。
然后,我尝试 post 使用以下准则将消息返回给客户端: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 网关
- 确保端点格式正确:
https://{api-id}.execute-api.{region}.amazonaws.com/{stage}/@connections/{connection_id}
如何才能成功发送消息给客户端?
原来是这条线
endpoint := fmt.Sprintf("https://%s/%s/@connections/%s", domain, stage, connectionID)
需要变成这个
endpoint := fmt.Sprintf("https://%s/%s/", domain, stage)