由于websocket是api网关提供的一项新功能,因此我很难找到有关在JAVA中使用它的文档或示例。我只能参考nodejs示例,在aws sdk中为Java寻找类似的api。最后,我可以建立连接,并且服务器(lambda函数)也可以从客户端接收消息。但是当我尝试从服务器向客户端发布消息时遇到了问题。
代码如下:
EndpointConfiguration endpointConfiguration = new EndpointConfiguration(
"myendpoint.execute-api.ap-southeast-1.amazonaws.com/develop/", "ap-southeast-1");
AmazonApiGatewayManagementApi api = AmazonApiGatewayManagementApiClientBuilder.standard()
.withEndpointConfiguration(endpointConfiguration).build();
PostToConnectionRequest request = new PostToConnectionRequest();
String connectionId = "myconnectionId";
request.withConnectionId(connectionId);
request.withData(ByteBuffer.wrap("tset".getBytes()));
api.postToConnection(request);
错误信息:
Exception in thread "main" com.amazonaws.AmazonServiceException: Unable to unmarshall exception response with the unmarshallers provided (Service: AmazonApiGatewayManagementApi; Status Code: 410; Error Code: null; Request ID: 66ef081c-70c5-11e9-9319-c18364b84c32)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1712)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1367)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1113)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:770)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:744)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:726)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:686)
at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:668)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:532)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:512)
at com.amazonaws.services.apigatewaymanagementapi.AmazonApiGatewayManagementApiClient.doInvoke(AmazonApiGatewayManagementApiClient.java:273)
at com.amazonaws.services.apigatewaymanagementapi.AmazonApiGatewayManagementApiClient.invoke(AmazonApiGatewayManagementApiClient.java:240)
at com.amazonaws.services.apigatewaymanagementapi.AmazonApiGatewayManagementApiClient.invoke(AmazonApiGatewayManagementApiClient.java:229)
at com.amazonaws.services.apigatewaymanagementapi.AmazonApiGatewayManagementApiClient.executePostToConnection(AmazonApiGatewayManagementApiClient.java:195)
at com.amazonaws.services.apigatewaymanagementapi.AmazonApiGatewayManagementApiClient.postToConnection(AmazonApiGatewayManagementApiClient.java:167)
at com.XXX.XXX.XXX.api.handler.WebsocketTestHandler.main(WebsocketTestHandler.java:114)
错误指向“ api.postToConnection(request);” 也许我的代码有问题,请帮助我或给我一些示例文档参考。