1
votes

I have written a simple Websocket application that has a server verticle and a client verticle. It runs perfectly fine on my local Windows system (Eclipse). The problem starts when it is deployed on AWS EC2(Ubuntu). While the server verticle seemingly has no problem and listens on the assigned port, when the Websocket client verticle, running on the same system attempts to connect to the server, it gets a connection refused message from the server. I even checked connecting to the Websocket from an external system through a browser - even that fails to connect. However, when I run an Http Server verticle on the same machine, I have no problem connecting to it and receiving message from the server. I have enabled all incoming traffic on all ports of the AWS instance. What is going wrong? Please help.

Here is the code:

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.http.WebSocket;


public class WebSocketServerApp {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        vertx.deployVerticle(new WebSocketServerVerticle());
        vertx.deployVerticle(new WebSocketClientVerticle("A"));
    }
}


class WebSocketServerVerticle extends AbstractVerticle {

    @Override
    public void start(Future<Void> startFuture) {
        HttpServer httpServer = vertx.createHttpServer();
        httpServer.websocketHandler(new Handler<ServerWebSocket>() {

            @Override
            public void handle(ServerWebSocket event) {
                System.out.println("Request received: " +event.binaryHandlerID());
                event.writeTextMessage("Message from WS server");
            }
        }).listen(9999);
    }
}

class WebSocketClientVerticle extends AbstractVerticle {

    String name;

    public WebSocketClientVerticle(String name) {
        this.name = name;
    }

    @Override
    public void start(Future<Void> startFuture) {
        HttpClient client = vertx.createHttpClient();
        client.websocket(9999, "localhost", "", new Handler<WebSocket>() {
            @Override
            public void handle(WebSocket ws) {

                System.out.println("WS Client: "+ ws.textMessageHandler(msg-> System.out.println("Received: "+msg)));
            }
        });
    }
}
1
Can you publish the stacktrace ?Hannoun Yassir
This is all I get in the console: Server started. Oct 23, 2017 11:26:15 AM io.vertx.core.http.impl.HttpClientImpl SEVERE: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:8080truthseeker

1 Answers

0
votes

Put a sleep between the two verticle deployments. Client verticle might be becoming up quicker than the server verticle becoming up.