【问题标题】:AWS - Using @connections websocket call back url to send response from back-end(one-way) - API Gateway websocket protocolAWS - 使用 @connections websocket 回调 url 从后端发送响应(单向) - API Gateway websocket 协议
【发布时间】:2019-10-25 13:20:05
【问题描述】:

我一直在努力通过 aws 提供的回调 url (https://******.execute-api.us-east-1.amazonaws.com/test/@connections) 向连接的客户端发送响应.

在我的后端有下面的代码来发送响应,这是一种通信方式。

    AwsClientBuilder.EndpointConfiguration config =
            new AwsClientBuilder.EndpointConfiguration("https://*********.execute-api.us-east-1.amazonaws.com/test", "us-east-1");
    AmazonApiGatewayManagementApi client = AmazonApiGatewayManagementApiClientBuilder.standard()
            .withEndpointConfiguration(config).build();


    PostToConnectionRequest request = new PostToConnectionRequest();
    request.withConnectionId("bGYcWek5oAMCKwA=");
    Charset charset = Charset.forName("UTF-8");
    CharsetEncoder encoder = charset.newEncoder();
    ByteBuffer buff = null;
    try {
        buff = encoder.encode(CharBuffer.wrap("Hello from lambda"));
    } catch (CharacterCodingException e) {
        log.error("Error while encoding", e);
    }
    request.withData(buff);
    PostToConnectionResult result = client.postToConnection(request);
    log.info("Result of websocket send:\n" + result);

获取服务:AmazonApiGatewayManagementApi;状态码:403;错误代码:InvalidSignatureException异常。

注意:我已经完成了所有设置,例如证书和 aws 访问密钥。

正如 aws 文档中提到的,我尝试过使用 awscurl ,它正在工作。我能够在 websocket 连接的客户端上看到响应。 https://docs.amazonaws.cn/en_us/apigateway/latest/developerguide/apigateway-how-to-call-websocket-api-connections.html

【问题讨论】:

    标签: java amazon-web-services websocket api-gateway


    【解决方案1】:

    您需要提供您的AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY 以形成BasicAWSCredentials

    BasicAWSCredentials awsCreds = new BasicAWSCredentials("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY");
    AmazonApiGatewayManagementApi client = AmazonApiGatewayManagementApiClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds)).withEndpointConfiguration(config).build();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-04
      • 2019-12-19
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 2020-02-11
      • 1970-01-01
      相关资源
      最近更新 更多