I am using socket.io-client.java and socket.io 1.2.1 on my node server for my android project and the android socket connects with the server fine but after some minutes it automatically disconnect and reconnect repeatedly. i can't figure out the problem, can someone help me?
i am using socket.io-client-0.1.1.jar, engine.io-client-0.2.1.jar and Java-WebSocket-1.3.0.jar libraries.
here is the java code
private void socketTest() throws URISyntaxException{
socket = IO.socket("http://192.168.169.2:8082");
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.emit("test", "awesome");
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {}
});
socket.connect();
}
and here is the server side code
io.on('connection', function (socket) {
console.log('a user connected');
socket.on('disconnect', function () {
console.log('user disconnected');
});
socket.on('test',function(msg){
console.log("This is "+msg);
});
});
and here is the log screenshot
I resolved this problem by adding an option with infinity timeout to socket.io
IO.Options options = new IO.Options();
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.connectTimeout(0, TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS)
.writeTimeout(0, TimeUnit.MILLISECONDS);
options.callFactory = clientBuilder.build();
socket = IO.socket(URL, options);
I have fetching same issue on socket.io-client:1.0.0
client library in my Android project but after the Downgrade the socket.io
version its work fine.Used below client version might be helpful you.
compile('io.socket:socket.io-client:0.8.3') {
exclude group: 'org.json', module: 'json'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With