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
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".
I stopped here, same problem like you all..
SSL, EVENT_CONNECT works, emit works too. Except socket.on("my-event",.....
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?
我遇到同样的问题,你解决了吗 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
Does anybody got solution for this Emitter.Listener not getting triggered? For me, only the Socket.EVENT_CONNECT listener is triggering
+1 this is terrible. Did anyone find a solution?
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 .
Having the same problem.
same boat here.. it works sometimes and sometimes it does not.. very inconvenient as in this state this can not be implemented for production
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
+1 Got exactly the same problem. No error message is shown.
+1 Same for me
+1 Still not working for me, however it worked in the past.
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);
}**
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.
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!
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
Any ideas?
10.21.2019 still problems. Any ideas?
still a problem:(