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

I'm in the same boat here. Manager.EVENT_TRANSPORT, Manager.ERROR, Socket.EVENT_CONNECT_ERROR and Socket.EVENT_ERROR are not being called and I don't know what else to do. Using "io.socket:socket.io-client:1.0.0".

alanwgt avatar Feb 02 '18 13:02 alanwgt

I stopped here, same problem like you all..

SSL, EVENT_CONNECT works, emit works too. Except socket.on("my-event",.....

muhrifqii avatar Feb 08 '18 04:02 muhrifqii

I also encountered the same problem. The callback of my registered event was not executed. I used it like the emit emitted by him. Can anyone help me?

chenguozhi avatar Apr 23 '18 08:04 chenguozhi

我遇到同样的问题,你解决了吗 I have the same problem, have you solved it?

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

chenguozhi avatar Apr 23 '18 08:04 chenguozhi

Does anybody got solution for this Emitter.Listener not getting triggered? For me, only the Socket.EVENT_CONNECT listener is triggering

venkat125 avatar Jul 23 '18 18:07 venkat125

+1 this is terrible. Did anyone find a solution?

maf1a avatar Jul 24 '18 08:07 maf1a

Cant believe its still an issue. I had migrated to custom methods long back. But if someone is down for guiding me. I would like to help see this fixed with a pull request. Anyone ?

On Tue, 24 Jul 2018 at 13:31, Den Nikitin [email protected] wrote:

+1 this is terrible. Did anyone find a solution?

— 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-407317644, or mute the thread https://github.com/notifications/unsubscribe-auth/AMzv-e2-cG5cAf6uUR0TsvUTwrxUNrhzks5uJtRxgaJpZM4G8rKX .

kevz93 avatar Jul 24 '18 17:07 kevz93

Having the same problem.

uzairamer avatar Jul 29 '18 16:07 uzairamer

same boat here.. it works sometimes and sometimes it does not.. very inconvenient as in this state this can not be implemented for production

msedek avatar Jul 30 '18 17:07 msedek

Same here.. as for our experiments it seems to be related to the network you are connected, so maybe it is a problem related to some timeout.. not sure.. and yes.. it is an intermitent issue for us

salvadomingo avatar Jul 31 '18 06:07 salvadomingo

+1 Got exactly the same problem. No error message is shown.

awsp avatar Aug 10 '18 02:08 awsp

+1 Same for me

nisiode avatar Aug 10 '18 06:08 nisiode

+1 Still not working for me, however it worked in the past.

eugenedw avatar Feb 13 '19 00:02 eugenedw

Try This if you are using https server

//Dependencies: implementation('io.socket:socket.io-client:1.0.0') { exclude group: 'org.json', module: 'json' } //Code:

**try {

        String socketUrl = "Your URL";
        HostnameVerifier hostnameVerifier = new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {

            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {

            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[0];
            }
        }};
        X509TrustManager trustManager = (X509TrustManager) trustAllCerts[0];

        SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, null);
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .hostnameVerifier(hostnameVerifier)
                .sslSocketFactory(sslSocketFactory, trustManager)
                .build();

        IO.Options opts = new IO.Options();
        opts.callFactory = okHttpClient;
        opts.webSocketFactory = okHttpClient;
        mSocket = IO.socket(socketUrl, opts);
        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.on("new message", onNewMessage);
        //Other Methods
        mSocket.connect();
    } catch (URISyntaxException | NoSuchAlgorithmException | KeyManagementException e) {
        throw new RuntimeException(e);
    }**

terminatorvishal avatar Feb 14 '19 10:02 terminatorvishal

Thanks for the idea. Tried it and doesn't work. Further debugging shows that the connection is timing out.

Exact underlying message: "xhr poll error"

One thing I'm investigating with my configuration pertains to redirects and proxies. My socket.io server sits behind an Apache load balancer using a reverse proxy configuration where the SSL certs for virtual hosts are centrally managed. When I'd tried enabling the port from the internal server to work directly over http (instead of https), the ping-pong occurs without issue.

eugenedw avatar Feb 14 '19 23:02 eugenedw

Also having the very same problem as everyone else. Emit works fine, but callback listener doesn't.

I've tried with both io.socket:socket.io-client:1.0.0 and com.github.nkzawa:socket.io-client:0.5.2.

More than 3 yrs since this issue was open and the problem still persists. Very frustrating!

roundsmapp avatar Mar 14 '19 00:03 roundsmapp

I just found my solution by debugging the library, I just found if my socket.io server endpoint is not set to addresss:port, like http://www.xxx.com/xxx/socket.io, and I passed the url into IO like this IO.socket(serverUrl, opts), it would just give me and default namespace called xxx/socket.io, then shit happened!!!!!, just FYI

RunziiMo avatar Jun 14 '19 16:06 RunziiMo

Any ideas?

elvus avatar Aug 07 '19 12:08 elvus

10.21.2019 still problems. Any ideas?

nhphung216 avatar Oct 21 '19 03:10 nhphung216

still a problem:(

yachash1594 avatar Dec 03 '19 08:12 yachash1594