4

I've been working with using socket.io with Android for a few days now. As of now, I am using AndroidAsync by Koush. When I try to connect to my local socket.io server (http://192.168.2.1:3000) everything is okay, I can emit commands and receive event messages. But when I try to use it to a live server with query string parameters (http://api.mysite.com:8000/socket.io/1?v=1&name=xxx&password=xxx) I cannot get to connect. Is there a proper way of passing query strings parameter to AndroidAsync socket.io? Here is my code.

    Uri.Builder b = Uri.parse("http://api.mysite.com:8000/socket.io/1").buildUpon();
    b.appendQueryParameter("v", "1");
    b.appendQueryParameter("name", "xxx");
    b.appendQueryParameter("pass", "xxx");

    myUrl = b.build().toString();

    AsyncHttpClient.getDefaultInstance().getString(myUrl, new AsyncHttpClient.StringCallback() {

            @Override
            public void onCompleted(Exception arg0, AsyncHttpResponse arg1, String arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onConnect(AsyncHttpResponse response) {
                // TODO Auto-generated method stub
                super.onConnect(response);
                Log.d("tag","onConnect!");
            }

            @Override
            public void onProgress(AsyncHttpResponse response, int downloaded,
                    int total) {
                // TODO Auto-generated method stub
                super.onProgress(response, downloaded, total);
                Log.d("tag","Progress!");
            }


        });

        SocketIOClient.connect(AsyncHttpClient.getDefaultInstance(), myUrl, new ConnectCallback() {

            @Override
            public void onConnectCompleted(Exception arg0, SocketIOClient client) {
                // TODO Auto-generated method stub
                if (client.isConnected()) {
                    Log.d("tag","!");
                } else {
                    Log.d("tag","?");
                }


            }
        });
SleepNot
  • 2,982
  • 10
  • 43
  • 72
  • "I cannot get to connect." means you can't even establish the socket, you can establish but you can't read, it gives timeout...? – nKn Feb 11 '14 at 12:37
  • I think no connection at all. Because on SocketIOClient.connect, I am checking for client.isConnected but is always returning false. – SleepNot Feb 12 '14 at 02:24
  • Changing from `127.0.0.1` to `api.mysite.com` seems that you're not using your LAN anymore and you're connecting to that server using internet, am I wrong? or do you have a local DNS service resolving that host to a local IP address? – nKn Feb 12 '14 at 07:45
  • It is actually being served over the internet. – SleepNot Feb 12 '14 at 07:50
  • Have you checked whether the 8000 port is opened up at your router and redirected to the correct local device? – nKn Feb 12 '14 at 07:51
  • Actually, I've already tried connecting to my server using Gottox Socket.io-java-client which can connect to the server but could not receive any response from the server when emitting commands. I am now trying out AndroidAsync which looks better but I think has complications/not working when connecting with query string parameters hence this question. – SleepNot Feb 12 '14 at 07:57

1 Answers1

1

I found this same issue in his other library android-websockets. I helped him fix it in both of his projects. Check out the changes made to AndroidAsync and android-websockets. The link to the android-websockets describes the problem in detail. I suggest you pull the latest changes in from the master branch and try again.

rarp
  • 1,122
  • 10
  • 20
  • You did mention in there to use Uri.builder, am I doing it right in my question or is there another way to set query parameters or add headers (instead of using query parameters) in AndroidAsync? – SleepNot Feb 12 '14 at 09:46
  • @jeraldov Your method call is correct, its just in the AndroidAsync library, the url was being screwed up by not using the Uri.Builder. – rarp Feb 12 '14 at 10:37
  • Wow. Your answer did connect me to my server. Many thanks! But I hope your help doesn't end here. Any chance we can discuss more about Socket.IO & Android? I would like to ask a few more questions regarding AndroidAsync. – SleepNot Feb 12 '14 at 10:52
  • Sure. Come chat with me in this room http://chat.stackoverflow.com/rooms/47308/socket-io-android – rarp Feb 12 '14 at 10:55
  • Hi @Rahul, do you happen to know if Android-websockets automatically connects on disconnection/timeout? – SleepNot Feb 20 '14 at 01:46
  • Hey @jeraldov, I believe it does not. You can always try again in the DisconnectCallback. I have a BroadcastReceiver setup to detect networking changes and try to connect to the socket when the network is connected, if the socket was disconnected. – rarp Feb 20 '14 at 02:59
  • I see. I am using the SocketIOClient in an Application class of Android to keep it alive for the whole app. So I only call SocketIOClient.connect() once and not sure how to call it again. Is this the right approach? – SleepNot Feb 20 '14 at 04:19
  • I keep a reference to the returned SocketIOClient when you connect, and when it disconnects, I reset it to null. So only call connect when the SocketIOClient variable is null. Otherwise it will make multiple socket connections. – rarp Feb 20 '14 at 05:16
  • I get what you say but dont know how to exactly do it. Because in my case, using the SocketIOClient in an Application class along with a Handler that receives messages from the client gets quite messy. Another thing Im also concerned about is I get too many views/variables from other activities public so my Application class Handler can access them. – SleepNot Feb 21 '14 at 10:18
  • I built a static class that needs the application context to connect the socket. After that, I don't need to use the application class, everything can be referenced statically through the SocketIOClient static variable. – rarp Feb 25 '14 at 03:43