使用 VertX HttpClient 访问 AWS WebSocket

时间:2021-08-01 17:05:39

标签: amazon-web-services websocket amazon-api-gateway vertx-httpclient

我在 AWS 上创建了一个带有 Web 套接字的 API 网关。我想使用 VertX 提供的 HttpClient 连接到它。我正在为客户端垂直使用以下代码:

public class WebSocketClient extends AbstractVerticle {

// application address replaced by [address]
protected final String host = "[address].execute-api.us-east-1.amazonaws.com";
protected final String path = "/dev";
protected final int port = 80;
protected final String webSocketAddress = "wss://[address].execute-api.us-east-1.amazonaws.com/dev";

@Override
public void start() throws Exception {
    startClient(this.vertx);
}

protected void startClient(Vertx vertx) {
    HttpClient client = vertx.createHttpClient();
    client.webSocket(port, host, path, asyncWebSocket -> {
        if (asyncWebSocket.succeeded()) {
            WebSocket socket = asyncWebSocket.result();
            System.out.println("Successfully connected. Node closing.");
            socket.close().onFailure(throwable -> {
                throwable.printStackTrace();
            });
        } else {
            asyncWebSocket.cause().printStackTrace();

        }
    });
 }
}

当我使用运行在 localhost 上的 VertX 服务器对其进行测试时,相同的代码有效,因此我认为这是正确的 WebSocketConnectionOptions 的问题。

当我尝试使用 HttpClient verticle 连接到 AWS 套接字时,出现“连接被拒绝”错误。使用 wscat 连接到它没有问题。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

This question 正在处理基本相同的问题。我将在此处发布解决方案,以记录将 AWS ApiGateway Websockets 与 VertX 结合使用的直接方法。

因此,我们的目标是实现连接到部署的 AWS Api WebSocket 网关的 VertX WebClient,该网关可以在 WsUri "wss://[address].execute-api.us-east-1.amazonaws.com 下访问/dev"(您必须将 [address] 替换为您的 ApiGateway Websocket 的地址)。

这里是设置WebClient,连接到Websocket,打印成功信息,然后再次断开连接的代码:

public class WebSocketClient extends AbstractVerticle {

protected final String webSocketUrl = "wss://[address].execute-api.us-east-1.amazonaws.com/dev"    

protected final String host = "[address].execute-api.us-east-1.amazonaws.com";
protected final String path = "/dev";
protected final int sslPort = 443;


@Override
public void start() throws Exception {
    startClient(this.vertx);
}

protected void startClient(Vertx vertx) {
    HttpClient client = vertx
            .createHttpClient(new 
HttpClientOptions().setDefaultHost(host).setDefaultPort(sslPort).setSsl(true));

    // connect to the web socket
    client.webSocket(path, asyncWebSocket -> {            
        if (asyncWebSocket.succeeded()) {
            // executed on a successful connection
            WebSocket socket = asyncWebSocket.result(); // use this for further communication                
            System.out.println("Successfully connected. Closing the socket.");
            // Closing the socket
            socket.close().onFailure(throwable -> {
                throwable.printStackTrace();
            });
        } else {
            // executed if the connection attempt fails
            asyncWebSocket.cause().printStackTrace();
        }
    });
}

您可以使用以下类来运行示例:

public class PlayWebSocket {
public static void main(String[] args) throws URISyntaxException{
    Vertx vertx = Vertx.vertx();
    WebSocketClient clientVerticle = new WebSocketClient();
    vertx.deployVerticle(clientVerticle);
  }
}

在 Java 端,这应该打印有关成功连接和关闭套接字的消息。在 AWS 端,应该调用 ApiGateway 的 $connect 和 $disconnect 方法。您可以使用 CloudWatch 在处理程序函数的日志中检查这一点。