java - AWS - 使用 @connections websocket 回调 url 从后端发送响应(单向) - API Gateway websocket 协议(protocol)

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

我一直在努力通过 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 异常(exception)。

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

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

最佳答案

您需要提供您的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();

关于java - AWS - 使用 @connections websocket 回调 url 从后端发送响应(单向) - API Gateway websocket 协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56538274/

相关文章:

javascript - Websockets-多种消息类型

java - 如何使用jsoup提取电子邮件ID?

java - dynamoDB 表上的 UpdateTableSpec 不起作用

amazon-web-services - 电子邮件地址未验证 (AWS SES)

amazon-web-services - AWS cognito - 我们可以修改 Amazon Cognito 在使用 google provider 进行身份验证时提供的重定向 URL

javascript - 我想根据 URL 创建多个命名空间,但我对 socket.io 感到困惑

javascript - Socket.IO 在 Node 上具有基本身份验证

Java getElapsedTime 在简单秒表中,返回枚举

java - Spring 安全映射

java - 读取我的 .jar 中的文本文件