user3740387
user3740387

Reputation: 367

How to keep sending messages in websockets even when the network is down?

I am using the java implementation of websockets(org.java_websocket). I am using "ifconfig l0 down" to simulate a network failure. I would like the server to keep sending messages even when the network is down and resend them(through tcp mechanism) once the network is up again. However, the java implementation has the following check in the send function

private void send( Collection<Framedata> frames ) {
    if( !isOpen() )
        throw new WebsocketNotConnectedException();

which leads to

an error occured
close a stream
Exception in thread "main" org.java_websocket.exceptions.WebsocketNotConnectedException

as soon as I simulate connectivity loss between the server and client.

Since I am not properly calling the close() function in websockets I feel the TCP mechanism should work for some time before timing out leading to websockets layer to close the connection. Am I expecting something outwordly? Is there an implementation that can help me?

Upvotes: 2

Views: 1278

Answers (3)

Abolfazl Miadian
Abolfazl Miadian

Reputation: 3159

It is so late for answering to this question, but I think my answer can help another guys: you should use

connectBlocking() 

instead of

connect()

function. note that connectBlocking() , block your thread!

Upvotes: 0

Zaboj Campula
Zaboj Campula

Reputation: 3360

It is not a good idea to simulate network connectivity problems by ifconfig down because the operating system knows the interface status. The OS then may (and it does) a handle connectivity error and an interface error differently and a network application gets different error indication.

An option how to simulate a connectivity errors on a linux box is iptables. Let say your application uses the port 80. You can drop all communication to/from port 80 via

iptables -A INPUT -p tcp --destination-port 80 -j DROP
iptables -A OUTPUT -p tcp --source-port 80 -j DROP

The traffic drop in iptables is handled like a network outage.

Upvotes: 1

Matthias247
Matthias247

Reputation: 10416

I think your test setup here does not really do what you want. I guess what happens is that as soon as you are using ifconfig the operating system is smart enough to look up which sockets are bound to this interface and closes them. Thereby Java and the websocket application get notified about the closed socket and the websocket connection gets cleaned up.

You probably need another way to simulate an "unplugged" connection, in the sense of you want to see a high packet loss/latency but without an actual connection close notification.

Upvotes: 0

Related Questions