【问题标题】:AndroidAsync websockets not workingAndroidAsync websockets 不工作
【发布时间】:2014-12-16 19:48:02
【问题描述】:

我正在使用 koush 的这个 AndroidSync 库来创建 websocket(服务器/客户端)并在两个 android 设备之间传输数据。两个设备通过wifi连接(一个是Wifi AP,另一个连接到它)。发送请求 4-5 秒后,我在客户端设备中收到 TimeoutException。 这就是我到目前为止所做的..

ServerActivity.java

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_server);

    mSockets = new ArrayList<WebSocket>();
    mAsyncHttpServer = new AsyncHttpServer();
    mWebSocketCallback = new AsyncHttpServer.WebSocketRequestCallback() {
        @Override
        public void onConnected(final WebSocket webSocket, RequestHeaders headers) {
            mSockets.add(webSocket);
            webSocket.send("Welcome Client");
            webSocket.setClosedCallback(new CompletedCallback() {
                @Override
                public void onCompleted(Exception ex) {
                    try {
                        if (ex != null)
                            Log.e("WebSocket", "Error");
                    } finally {
                        mSockets.remove(webSocket);
                    }
                }
            });
            webSocket.setStringCallback(new WebSocket.StringCallback() {
                @Override
                public void onStringAvailable(String s) {
                    Log.d("SERVERTAG",s);
                    Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
                }
            });
        }
    };

    mAsyncHttpServer.websocket("/",mWebSocketCallback);
    mAsyncHttpServer.listen(Utils.PORT_NUMBER);

    Button sendButton = (Button) findViewById(R.id.sendButtonS);
    sendButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            for(WebSocket socket : mSockets) {
                socket.send("Server sent a string");
            }
        }
    });

}

ClientActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_client);
    mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    //Resolve IP address
    int ipAddress = mWifiManager.getConnectionInfo().getIpAddress();
    String hostAddress = Formatter.formatIpAddress(ipAddress);
    hostAddress = "http://" + hostAddress + ":" +Utils.PORT_NUMBER;
    Log.d("CLIENTTAG", "address is " + hostAddress);

    mWebSocketConnectCallback = new AsyncHttpClient.WebSocketConnectCallback() {
        @Override
        public void onCompleted(Exception ex, WebSocket webSocket) {
            if (ex != null) {
                ex.printStackTrace();
                return;
            }
            webSocket.send("Hello Server");
            webSocket.setStringCallback(new WebSocket.StringCallback() {
                @Override
                public void onStringAvailable(String s) {
                    Log.d("CLIENTTAG",s);
                    Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
                }
            });
        }
    };
    mAsyncHttpClient = AsyncHttpClient.getDefaultInstance();
    mAsyncHttpClient.websocket(hostAddress, null, mWebSocketConnectCallback);

}

这是我在客户端设备的 logcat 中得到的。

10-21 19:50:49.289      742-945/com.haloappstudio.musichub W/System.err﹕ java.util.concurrent.TimeoutException
10-21 19:50:49.289      742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.http.AsyncHttpClient$2.run(AsyncHttpClient.java:240)
10-21 19:50:49.289      742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer.lockAndRunQueue(AsyncServer.java:683)
10-21 19:50:49.289      742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:700)
10-21 19:50:49.289      742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:608)
10-21 19:50:49.289      742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer.access$700(AsyncServer.java:37)
10-21 19:50:49.289      742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer$13.run(AsyncServer.java:557)

我以前没有真正做过套接字编程。有谁能帮帮我吗?

感谢任何帮助。

【问题讨论】:

    标签: java android websocket socket.io androidasync-koush


    【解决方案1】:

    我发现了问题,正如@jrandaz 所说,问题出在服务器的 IP 地址上。

    结果

    WifiManager.getConnectionInfo().getIpAddress()

    返回设备自己的 IP 地址,而不是它所连接的 wifi 热点设备的地址。 我使用了192.168.43.1,这是android中wifi热点的默认IP地址,它可以工作。

    【讨论】:

      【解决方案2】:

      在客户端,你有没有试过在你有 null 的地方传递 http?

      mAsyncHttpClient.websocket(hostAddress, null, mWebSocketConnectCallback);

      不妨试试

      mAsyncHttpClient.websocket(hostAddress, http, mWebSocketConnectCallback);

      另外,当您提到您的两个设备都连接到 Wifi 时,您所说的第二个设备是“连接到它”是什么意思。 (将通过评论澄清这一点,但没有足够的分数)连接到演示服务器时,您可能没有收到正确的 IP 地址。

      【讨论】:

      • 我的意思是第二台设备连接到第一台设备的wifi热点。
      猜你喜欢
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 2022-12-14
      • 2011-05-22
      • 1970-01-01
      • 2021-12-04
      • 2015-06-15
      • 2016-08-13
      相关资源
      最近更新 更多