【问题标题】:"Connection refused" Autobahn Android to Java EE Endpoint“连接被拒绝”Autobahn Android 到 Java EE 端点
【发布时间】:2017-01-04 11:34:30
【问题描述】:

我在 Payara 服务器上设置了 Java EE Endpoint,我尝试使用 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();
   }

}

现在可以通过 ws://localhost:46588/Server/WebSocket 访问 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 客户端时,套接字关闭而无法建立连接并显示“连接被拒绝”消息。我在onCreate中使用如下代码进行连接(mSocket是activity类的一个字段):

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>

我的代码或方法中有什么错误吗?是否可以记录/捕获连接事件(握手或其他什么)以阐明连接被拒绝的原因?

【问题讨论】:

    标签: java android jakarta-ee autobahn


    【解决方案1】:

    当我准备发布我上次搜索的问题时:lo and behold,localhost 不是运行模拟器的机器的 localhost(我猜它是模拟设备的 localhost)。所以使用:

    10.0.2.2:[port] 
    

    而不是

    localhost:[port] 
    

    在模拟器上会很好(如果在实际设备上运行,请不要忘记更改)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 2015-06-15
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 2017-08-08
      • 2011-07-01
      相关资源
      最近更新 更多