Socket listeners not working but emit does.
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
Your Socket.EVENT_CONNECT event listener is being called?
not at all =/
Any errors on console?
Nopes
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.
SSL? Are you using HTTPS?
I was. I switched back and no difference.
@kevz93 is it still an issue? Actually, I have no idea.
Yes it is. Im running it as an Service and it doesnt work. =/
@kevz93 Did you get the chance to make your listeners work?
Nopes. Had to move to an alternative.
I'm facing the exact same issue where everything works except listening on my events.
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
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 .
same here, got advise?
SSL, EVENT_CONNECT works, emit too, but no socket.on("something".....
@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'
}
I have same issue.
Any solution?
Any Solution???? connect, emit working but on listener not working.
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.
Do you have any solutions or any idea how to overcome? I am facing the same problem. socket.on(... doesn't work