"Connection refused" 高速公路 Android 到 Java EE 终点

"Connection refused" Autobahn Android to Java EE Endpoint

我在 Payara 服务器上设置了一个 Java EE 端点,我尝试使用 Autobahn WebSockets 连接到一个 Android 客户端。我有以下设置:

我在服务器上的 WebSocket 端点:

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

我这样注册 WebSocket:

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

}

WebSocket 现在可以访问 att ws://localhost:46588/Server/WebSocket。我已经确认它适用于 chrome 中的以下 javascript:

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!");
   }
}

然而,当我尝试使用高速公路连接我的 android 客户端时,套接字关闭,但无法与消息 "Connection refused" 建立连接。我在onCreate中使用如下代码连接(mSocket是activityclass的一个字段):

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;
}

我的清单是这样的:

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

我的代码或方法有什么错误吗?是否可以 log/catch 连接事件(握手或不握手)以阐明连接被拒绝的原因?

当我准备 post 我上次用谷歌搜索的问题时:lo and behold,localhost 不是机器的本地主机 运行 模拟器(我猜是模拟设备的本地主机)。所以使用:

10.0.2.2:[port] 

而不是

localhost:[port] 

在模拟器上就可以了(如果在实际设备上运行不要忘记更改)