How to keep the TCP client connected with TCP server forever

1,087 views
Skip to first unread message

Shaheer Thooppil

unread,
Nov 21, 2014, 7:17:56 AM11/21/14
to ve...@googlegroups.com
Hi all,
I am creating a chat application. In the client section i would like to keep the connection alive throughout the life of the application. 
The Client need to reconnect to the server automatically when the server is restarted or when ever the server is up and running. 
How i can achieve this using vertx NetClient

Any Samples? Thanks in advance.

Alexander Lehmann

unread,
Nov 21, 2014, 7:38:51 AM11/21/14
to ve...@googlegroups.com
Do you mean a browser/server type chat? In this case, take a look at websockets and sockjs.

If you mean server/server communication like a service bus, the easiest solution is probably to use one or you will have to implement the functionality yourself (ping packets, retries etc).

Tim Fox

unread,
Nov 21, 2014, 7:45:06 AM11/21/14
to ve...@googlegroups.com
NetClient can handle reconnect, please take a look at the docs for more info.
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shaheer Thooppil

unread,
Nov 21, 2014, 8:00:54 AM11/21/14
to ve...@googlegroups.com
Hi Tim
I have  tried like this

NetClient client = vertx.createNetClient();

client.setReconnectAttempts(1000);
client.setReconnectInterval(500);

But when i restarted the server, the client is not reconnecting.

Tim Fox

unread,
Nov 21, 2014, 8:04:23 AM11/21/14
to ve...@googlegroups.com
Do you have a reproducer?

Shaheer Thooppil

unread,
Nov 21, 2014, 8:20:49 AM11/21/14
to ve...@googlegroups.com
Please find the attached file.

--
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/QuJn4ZmUaw0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+un...@googlegroups.com.
SimpleTCPClient.java
SimpleTCPServer.java

Tim Fox

unread,
Nov 21, 2014, 8:25:31 AM11/21/14
to ve...@googlegroups.com
Ah right. reconnectAttempts is how many attempts to make when connecting to the server, it doesn't mean the client will automatically reconnect itself.

To do that you need to specify closeHandler/exceptionHandler and do the reconnect yourself.

Shaheer Thooppil

unread,
Nov 21, 2014, 8:42:16 AM11/21/14
to ve...@googlegroups.com
 That is what i m looking for.. What code i should write to reconnect?
Is there any method like socket.reconnect() some thing like that. Which i can call inside closeHandler like:

socket.closeHandler(new VoidHandler() {
@Override
protected void handle() {
log.info("Server Disconneted ");
socket.reconnect();
}
});
Reply all
Reply to author
Forward
0 new messages