2
votes

I have a Vert.X SockJS server running using the following code:

HttpServer httpServer = vertx.createHttpServer();
SockJSServer sockJSServer = vertx.createSockJSServer(httpServer);
JsonObject config = new JsonObject().putString("prefix", "/echo");

sockJSServer.installApp(config, new Handler<SockJSSocket>() {
    public void handle(SockJSSocket sock) {
        Pump.createPump(sock, sock).start();
    }
});

httpServer.listen(8080);

Now I need to send messages from the server to an Android (and vice versa) application and I have no idea how to set that up on the client. The documentation talks about handling that in JavaScript but on the browser.

UPDATE: I believe the following code is a bit in the right direction. I still need to add the host ip address (not sure how).

public void start() {
    SockJSSocket client = new SockJSSocketBase(vertx){

        @Override
        public boolean writeQueueFull(){
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public SockJSSocket setWriteQueueMaxSize(int arg0){
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public SockJSSocket drainHandler(Handler<Void> arg0){
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public SockJSSocket write(Buffer arg0){
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public SockJSSocket exceptionHandler(Handler<Throwable> arg0){
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public SockJSSocket resume(){
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public SockJSSocket pause(){
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public SockJSSocket dataHandler(Handler<Buffer> arg0){
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public SockJSSocket endHandler(Handler<Void> arg0){
            // TODO Auto-generated method stub
            return null;
        }
    };
}

Thanks in advanced!

1

1 Answers

0
votes

I think you can try a different way, if you want your android app to talk to a vertx server: using websocket, it is easier than using sockjs to setup a connection between them. Because I met and solved a similar requirement not long ago.

Because websocket connection is a two-way channel, which means a vertx app could send text to your android app and vice versa. At the same time, there is Java-WebSocket(http://java-websocket.org/) which you can use for your android app. It is very easy to use.

Also, to set up a websocket handler in vertx is not hard, there are many examples in document.

Good luck, :)