如何解决AWS API Gateway WebSockets [POST] @connections返回404未找到
我将一个(或几个)客户端连接到API网关中的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,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
}
是本地调用发送函数还是客户端发送消息都没有关系,API网关调用我的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/",stage)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。