socket.io-client-java icon indicating copy to clipboard operation
socket.io-client-java copied to clipboard

Socket listeners not working but emit does.

Open kevz93 opened this issue 10 years ago • 60 comments

The .on listener never seem to be fired! I've tried multiple versions of socket IO. This is a fantastic library Ive used on javascript. dont know why Java seem to have a problem :( Ive tried extending the Service class. Weird thing is my emits work properly but my .on listeners don't seem to fire at all! Here is the code :

import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

public class SocketService extends Service {
    final String TAG="SocketService";
    JSONObject user_data;
    private Socket socket;

    private final IBinder mBinder = new MyBinder();
    public class MyBinder extends Binder {
        SocketService getService() {
            return SocketService.this;
        }
    }

    @Override
    public void onCreate(){
        super.onCreate();
        try {
            socket = IO.socket("http://serverIP:port/"); //this gets connected
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "onCreate");
        socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                Log.d(getClass().getCanonicalName(), "Connected to server");
            }

        }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... arg0) {
                Log.d(getClass().getCanonicalName(), "Disconnected from server");
            }

        });
        socket.on("friendcall", new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                JSONObject data = (JSONObject) args[0];
                Log.d(TAG, "Handling friendcall");
                try {
                    String callFrom = data.getString("from");
                    Log.d(TAG, "Call from : " + callFrom);
                } catch (JSONException e) {
                    Log.d(TAG, "friend call object cannot be parsed");
                }

            }
        });

        Log.d(TAG, "onStartCommand. Socket should be up");
        socket.connect();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Bundle extras = intent.getExtras();
        if(extras!=null){
            try {
                user_data = new JSONObject(extras.getString("USER_DATA"));
                socket.emit("giveMeID", user_data.getString("email"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return Service.START_NOT_STICKY;
    }


    @Override
    public IBinder onBind(Intent intent) {
        //TODO for communication return IBinder implementation
        return null;
    }

    @Override
    public void onDestroy(){
        socket.disconnect();
        super.onDestroy();
        Log.d(TAG,"onDestroy");
    }

}

lib version tried ,

compile ('io.socket:socket.io-client:0.6.3') {
        // excluding org.json which is provided by Android
        exclude group: 'org.json', module: 'json'
    }

Also tried the com.github.nkzawa:socket.io-client:0.3.0 package

kevz93 avatar Dec 30 '15 07:12 kevz93

Your Socket.EVENT_CONNECT event listener is being called?

wedneyyuri avatar Dec 30 '15 12:12 wedneyyuri

not at all =/

kevz93 avatar Dec 30 '15 12:12 kevz93

Any errors on console?

wedneyyuri avatar Dec 30 '15 12:12 wedneyyuri

Nopes

kevz93 avatar Dec 30 '15 12:12 kevz93

I also have server as IP and as a domain name with SSL. In the code I use the IP directly to connect. Ive read on issues popping if you give in the domain name.

kevz93 avatar Dec 30 '15 13:12 kevz93

SSL? Are you using HTTPS?

wedneyyuri avatar Dec 30 '15 14:12 wedneyyuri

I was. I switched back and no difference.

kevz93 avatar Dec 30 '15 17:12 kevz93

@kevz93 is it still an issue? Actually, I have no idea.

nkzawa avatar Jan 16 '16 05:01 nkzawa

Yes it is. Im running it as an Service and it doesnt work. =/

kevz93 avatar Jan 16 '16 10:01 kevz93

@kevz93 Did you get the chance to make your listeners work?

doctiger avatar Feb 23 '16 22:02 doctiger

Nopes. Had to move to an alternative.

kevz93 avatar Feb 24 '16 11:02 kevz93

I'm facing the exact same issue where everything works except listening on my events.

arielatdev avatar Apr 29 '16 10:04 arielatdev

I have a defined a callback like so private Emitter.Listener onLogin = new Emitter.Listener() { @Override public void call(final Object... args) { JSONObject data = (JSONObject) args[0]; Log.i("Logged in", data.toString()); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "Logged in : " + args.toString(), Toast.LENGTH_LONG).show(); Log.i("Logged in", args.toString()); } }); } };

and registered it like so

mSocket.emit("command", loginPayload);

The callback called onLogin doesn't get fired, but Socket.EVENT_CONNECT does get called. Please advise

arkangeluicentric avatar Jul 18 '16 11:07 arkangeluicentric

Define '.on' event name On Mon, 18 Jul 2016 at 4:42 PM, arkangeluicentric [email protected] wrote:

I have a defined a callback like so private Emitter.Listener onLogin = new Emitter.Listener() { @Override https://github.com/Override public void call(final Object... args) { JSONObject data = (JSONObject) args[0]; Log.i("Logged in", data.toString()); runOnUiThread(new Runnable() { @Override https://github.com/Override public void run() { Toast.makeText(MainActivity.this, "Logged in : " + args.toString(), Toast.LENGTH_LONG).show(); Log.i("Logged in", args.toString()); } }); } };

and registered it like so

mSocket.emit("command", loginPayload);

The callback called onLogin doesn't get fired, but Socket.EVENT_CONNECT does get called. Please advise

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/socketio/socket.io-client-java/issues/268#issuecomment-233301755, or mute the thread https://github.com/notifications/unsubscribe-auth/AMzv-VzQU-aCHnu-vxVKpDhHJoLc8LToks5qW1-SgaJpZM4G8rKX .

kevz93 avatar Jul 18 '16 12:07 kevz93

same here, got advise?

SSL, EVENT_CONNECT works, emit too, but no socket.on("something".....

true-gler avatar May 26 '17 22:05 true-gler

@true-gler I am facing the same issue, it event connects, and emits too, no SSL error too... but my custom events... it's not listening. Were you able to solve this issue?

my build.gradle file:

ext {
    androidSupportVersion = '25.3.1'
    daggerVersion = '2.10'
    butterknifeVersion = '8.5.1'
    retrofitVersion = '2.2.0'
    okHttpVersion = '3.7.0'
    leakcanaryVersion = '1.5'
    stethoVersion = '1.4.2'
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile('io.socket:socket.io-client:0.8.3') {
        exclude group: 'org.json', module: 'json'
    }
    compile "com.android.support:appcompat-v7:$androidSupportVersion"
    compile "com.android.support:design:$androidSupportVersion"
    compile "com.google.dagger:dagger:$daggerVersion"
    annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
    compile "com.jakewharton:butterknife:$butterknifeVersion"
    annotationProcessor "com.jakewharton:butterknife-compiler:$butterknifeVersion"
    compile "com.squareup.retrofit2:retrofit:$retrofitVersion"
    compile "com.squareup.retrofit2:converter-gson:$retrofitVersion"
    compile "com.facebook.stetho:stetho:$stethoVersion"
    compile "com.facebook.stetho:stetho-okhttp3:$stethoVersion"
    testCompile "com.squareup.leakcanary:leakcanary-android-no-op:$leakcanaryVersion"
    debugCompile "com.squareup.leakcanary:leakcanary-android:$leakcanaryVersion"
    compile "com.squareup.okhttp3:okhttp:$okHttpVersion"
    compile "com.squareup.okhttp3:logging-interceptor:$okHttpVersion"
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'org.greenrobot:eventbus:3.0.0'
    testCompile 'junit:junit:4.12'
}

arvi avatar Jun 05 '17 12:06 arvi

I have same issue.

Any solution?

emelyan1987 avatar Aug 25 '17 12:08 emelyan1987

Any Solution???? connect, emit working but on listener not working.

PreetiTeks avatar Aug 29 '17 09:08 PreetiTeks

I am facing the issue of listeners not working after reconnecting. Connect listener is only working but other listeners unable to listen sometimes. I am using 'io.socket:socket.io-client:1.0.0' in my gradle. Please provide a solution to this issue.

jay007minz avatar Dec 05 '17 16:12 jay007minz

Do you have any solutions or any idea how to overcome? I am facing the same problem. socket.on(... doesn't work

araratmna avatar Jan 25 '18 06:01 araratmna