Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.IO-client.java disconnect and reconnect repeatedly

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

enter image description here

like image 338
ShegaMike Avatar asked Jan 09 '15 11:01

ShegaMike


2 Answers

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);
like image 147
Afshin Avatar answered Nov 02 '22 17:11

Afshin


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'
}
like image 23
Sagar Jethva Avatar answered Nov 02 '22 19:11

Sagar Jethva