"Connection refused" Autobahn Android to Java EE Endpoint

612 views Asked by At

I have a Java EE Endpoint set up on a Payara server to which I attempt to connect an Android client using Autobahn WebSockets. I have the following setup:

My WebSocket Endpoint on the server:

public class CommunicationSocket extends Endpoint {

   @Override
   public void onOpen(Session aSession, EndpointConfig aConfig) {
      aSession.addMessageHandler(new MessageHandler.Whole<byte[]>() {
         @Override
         public void onMessage(byte[] aMessage) {
            // Do something fun
         }
      });
   }
}

I register the WebSocket as such:

public class RegisterSocket implements ServerApplicationConfig {

   @Override
   public Set<ServerEndpointConfig> getEndpointConfigs(
           Set<Class<? extends Endpoint>> aEndpointClasses) {
       Set<ServerEndpointConfig> result = new HashSet<>();
       for (Class endpointClass : aEndpointClasses) {
          if (endpointClass.equals(CommunicationSocket.class)) {
             ServerEndpointConfig sec
                       = ServerEndpointConfig.Builder.create(endpointClass,
                            "/WebSocket").build();
             result.add(sec);
          }
       }
       return result;
   }

   @Override
   public Set<Class<?>> getAnnotatedEndpointClasses(
           Set<Class<?>> aScanned) {
      return Collections.emptySet();
   }

}

The WebSocket can now be reached att ws://localhost:46588/Server/WebSocket. I've confirmed that it works with the following javascript in chrome:

function WebSocketTest() {
   if ("WebSocket" in window) {
      ws = new WebSocket("ws://localhost:46588/Server/WebSocket");

      ws.onopen = function() {
         ws.send("Message to send");
            alert("Message is sent...");
      };

      ws.onmessage = function (evt) { 
              var received_msg = evt.data;
              alert("Message is received... \n" + received_msg);
      };

   } else {
      // The browser doesn't support WebSocket
      alert("WebSocket NOT supported by your Browser!");
   }
}

However when I try to connect with my android client using autobahn, the socket closes without being able to establish a connection with the message "Connection refused". I use the following code in onCreate to connect (mSocket is a field of the activity class):

WebSocketHandler messageHandler = new WebSocketHandler() {
   @Override
   public void onOpen() {
      System.out.println("Connected...");
   }

   @Override
   public void onClose(int code, String reason) {
      System.out.println("Socket closing: " + reason);
      mSocket = null;
   }

   @Override
   public void onTextMessage(String payload) {
      System.out.println("Received: " + payload);
   }

   @Override
   public void onBinaryMessage(byte[] payload) {
      System.out.println("Message received: " + payload);          
   }
};

mSocket = new WebSocketConnection();
try {
   String address = "ws://localhost:46588/Server/WebSocket";
   mSocket.connect(address, messageHandler);
} catch (WebSocketException e) {
   System.out.println("Could not connect to WebSocket: " 
           + e.getMessage());
   mSocket = null;
}

I have internet permissions i my manifest as so:

<uses-permission android:name="android.permission.INTERNET">
</uses-permission>

Is there anything in my code or approach that is wrong? Is it possible to log/catch the event of connecting (handshake or what not) in order to shed light onto why the connection is refused?

1

There are 1 answers

0
Christian Eriksson On BEST ANSWER

And as I got ready to post the question I googled one last time: lo and behold, localhost is not the localhost of the machine running the emulator (I guess it's the localhost of the emulated device). So use:

10.0.2.2:[port] 

instead of

localhost:[port] 

and it will be fine on the emulator (don't forget to change if running on an actual device)