AndroidAsync websockets无法正常工作

时间:2014-10-21 14:27:54

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

我正在使用kush的这个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)

之前我还没有真正完成套接字编程。有人可以帮帮我吗?

感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

我发现了这个问题,因为@jrandaz说问题出在服务器的IP地址上。

结果

WifiManager.getConnectionInfo().getIpAddress()

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

答案 1 :(得分:0)

在客户端,您是否尝试过将http传递给null?

mAsyncHttpClient.websocket(hostAddress, null, mWebSocketConnectCallback);

也许试试

mAsyncHttpClient.websocket(hostAddress, http, mWebSocketConnectCallback);

此外,当您参考连接到Wifi的两个设备时,第二个设备的意思是“连接到它”&#39;。 (通过评论澄清这一点,但没有足够的分数)连接到演示服务器时,您可能没有收到正确的IP地址。

相关问题