使用 VertX HttpClient 访问 AWS WebSocket

Accessing AWS WebSocket using VertX HttpClient

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

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();

        }
    });
 }
}

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

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

非常感谢您的帮助。

处理的问题基本相同。我将 post 此处的解决方案只是为了记录使用 AWS ApiGateway Websockets 和 VertX 的直接方法。

因此,目标是实现一个连接到部署的 AWS Api WebSocket 网关的 VertX WebClient,可以在 WsUri“wss://[address].execute-api.us -east-1.amazonaws.com/dev”(您必须将 [address] 替换为 Api 网关 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();
        }
    });
}

您可以使用下面的class到运行的例子:

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

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