I am using socket.io-client 0.6.3 https://github.com/socketio/socket.io-client-java for making websocket client. I have written a connect method as below:-
public void connect(String host, int port) throws Exception {
socket = IO.socket(String.format(url, host, port));
//socket connect, disconnect, error event
subscribeEvents(socket);
socket.connect();
}
where subscribeEvents method is as below
void subscribeEvents(final Socket socket) {
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.emit(CommandType.JOINCOMMAND.getValue(), CLIENT_NAME);
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
errorDisconnectListener.onDisconnect();
}
}).on(InitialCommandValue.GETAVAILABLEPERSPSETIVESVALUE.getValue(), new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.emit(CommandType.RESPONSECOMMAND.getValue(), commandMessageListener.getPerspectives(args));
}
})
.on(Socket.EVENT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
errorDisconnectListener.onError();
}
}).on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... args) {
commandMessageListener.onMessage(args);
}
});
}
Now when I get the event disconnect or error due to network problem or the machine becomes unreachable, I shutdown the socket as below
this.socket.off();
this.socket.disconnect();
this.socket.close();
this.socket = null;
and calls the connect method again until the connect event gives the confirmation.
But the problem is that once the network comes in, all the socket connection comes back online and start giving the socket events(means I get the various duplicate events).
My requirement is that once the network goes away, dispose the socket and try connecting again and again until the connection succeeds(keepalive mechanism).
Can anyone helps me where I can be wrong?