繁体   English   中英

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

[英]AWS - Using @connections websocket call back url to send response from back-end(one-way) - API Gateway websocket protocol

我一直在努力通过 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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM