Keselme
Keselme

Reputation: 4249

Socket IO connection fails on Android

I'm trying to create a simple chat using Socket IO, Android and Node. When I run the app and try to connect to the server, it always fails with a timeout error and I can't figure out why.

Here's the Node code:

app = require('express')()
http = require('http').createServer(app)
io = require('socket.io')(http)

app.get('/', (req,res) => {

    res.send('Chat server is running on port 5000')
})

/**
 * Defines a listener to an event called connection and this event will be fired from the client side
 */
io.on('connection', (socket) => {

    console.log('user connected')
})

http.listen(5000, () => {

    console.log('Node app is running on port 5000')
})

Here's the Android code (Editted according to Marcos Casagrande Answer:

import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;
import com.github.nkzawa.engineio.client.transports.WebSocket;

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

    Intent intent = getIntent();
    if(intent.hasExtra(NICKNAME)){

        mNickname = intent.getStringExtra(NICKNAME);
        Log.d(TAG, "chatapp onCreate: " + mNickname);
    }

    try {

        IO.Options opts = new IO.Options();
        opts.transports = new String[]{WebSocket.NAME};
        mSocket = IO.socket("http://10.0.0.21:5000", opts);

        mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                Log.d(TAG, "chatapp call: connected to server");
            }
        });

        mSocket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                Log.d(TAG, " chatapp call: disconnected from the server");
            }
        });

        mSocket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                Log.d(TAG, "chatapp call: connection error");
            }
        });

        mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                Log.d(TAG, "chatapp call: connection timeout");
            }
        });

        mSocket.connect();

    } catch (Exception e) {
        Log.d(TAG, "onCreate: something unexpected happened");
        e.printStackTrace();
    }

I try to run the app on virtual device through Android Studio I keep getting Connection timeout error. Can someone tell me, what am I doing wrong?

Upvotes: 2

Views: 6927

Answers (2)

Sanush
Sanush

Reputation: 325

You have to use 2 AVD's instead of physical devices to connect with this IP. If now try with http://10.0.2.2:5000

Upvotes: 0

Marcos Casagrande
Marcos Casagrande

Reputation: 40404

The connection won't happen instantly, it's asyncrhonous, attach the correct handlers and check for connection or errors in there.

mSocket = IO.socket("http://localhost:5000");

mSocket.on(Socket.EVENT_CONNECT, onConnect);
mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);

mSocket.connect();
// ....

private Emitter.Listener onConnect = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.d(TAG, "connected...");
        // This doesn't run in the UI thread, so use: 
        // .runOnUiThread if you want to do something in the UI

    }
};

private Emitter.Listener onConnectError = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.d(TAG, "Error connecting...");
    }
};

I recommend using WebSocket transport in android, to avoid polling errors, you can check my answer here:

How to set transports for SocketIO in Android?

Upvotes: 4

Related Questions