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