【问题标题】:AWS API Gateway WebSockets [POST]@connections Returning 404 NotFoundAWS API Gateway WebSockets [POST]@connections 返回 404 NotFound
【发布时间】: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


    【解决方案1】:

    原来是这条线

    endpoint := fmt.Sprintf("https://%s/%s/@connections/%s", domain, stage, connectionID)

    需要变成这个

    endpoint := fmt.Sprintf("https://%s/%s/", domain, stage)

    【讨论】:

      猜你喜欢
      • 2016-02-29
      • 2017-09-27
      • 1970-01-01
      • 2019-06-05
      • 2016-02-10
      • 2019-08-24
      • 2023-03-14
      • 2017-10-26
      • 2015-10-15
      相关资源
      最近更新 更多