Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the java websocket client can't reseived message after 30mins #213

Closed
gguava opened this issue Oct 11, 2013 · 9 comments
Closed

the java websocket client can't reseived message after 30mins #213

gguava opened this issue Oct 11, 2013 · 9 comments

Comments

@gguava
Copy link

gguava commented Oct 11, 2013

the java websocket client can't reseived message after 30mins,
is there any option to set the connect time out

@xdansmith
Copy link

I'm having a similar issue in another library that uses Java-WebSocket. It doesn't take 30 minutes for it to happen, but if nothing happens for a short period of time (say, 5 minutes), then the connection remains open but the connection quits receiving data.

This is happening to me when using the library in an Android app, so I don't know if this is perhaps an Android specific problem?

I can see you posted this a couple weeks ago - have you made any progress on finding a solution?

@Joris-van-der-Wel
Copy link

Unless something else is going on, this seems to be a well known issue that is unrelated to websockets.
TCP itself does not drop connections after a timeout by default, the only way to know if your connection is still alive is by sending data.
However a lot of firewalls, routers, et cetera, drop TCP connections if no data has been sent for a while. The only way to get around this is to send some data yourself from time to time. (also see http://stackoverflow.com/questions/3907537/keep-alive-tcp-ip-connected-sockets-over-the-internet-when-how-and-how-much/5149662#5149662 )

The WebSocket protocol provides a ping operation that lets you do this ( http://tools.ietf.org/html/rfc6455#section-5.5.2 ), but you could also accomplish this in your application protocol. Using ping in the WebSocket protocol might be more convenient because it requires no changes in your application protocol.

This library implements ping and pong, but it does not provide a convenient single method to send pings, it does provide ping/pong listeners. This is how you send a Ping operation:

void sendPing(WebSocket conn)
{
        conn.sendFrame(new FramedataImpl1(Opcode.PING));
}

After you do this, the other party will immediately reply with a pong.

What you could do is:

  1. Keep a variable that stores the last time you received a message. You set this variable by implementing and overriding the methods in WebSocketListener/WebSocketAdapter such as onWebsocketMessage, onWebsocketPing, onWebsocketPong, onWebsocketOpen.
    In your main loop, whenever this variable older than X seconds (maybe a few minutes?), you can assume the connection is dead.
  2. In your main loop, Send a Ping every X seconds (maybe 30?)

@xdansmith
Copy link

@Joris-van-der-Wel,

Do you think this is something that consumers of this library should have to do, or is this something that should be implemented in the Java-WebSocket library itself? It seems like sending out Ping requests ought to be handled by this library rather than the consumer.

Regardless, I really appreciate you providing advice on how to go about implementing this without making any changes to this library, and I may end up doing just that if the library I'm using doesn't implement logic similar to this or this library doesn't get updated to do it on its own.

@Joris-van-der-Wel
Copy link

As long as it is optional, java-websocket would seem to be the right place to me yes. Or at least make it easier to implement, for example by adding a few methods on the WebSocket interface:

  • getter that returns the time (nanoTime) that the readyState was last changed (so that you can handle connect timeouts)
  • getter that returns when the last message was sent
  • getter that returns when the last message was received
  • getter that returns WebSocket.Role so that you can find out if we are a server or client (this is very useful if you are making a library on top of the java-websocket lib)
  • a sendPing() method

@ghost
Copy link

ghost commented Feb 3, 2014

This doesn't seem to work when sent from WebSocketClient.

Just an FYI. I don't even know if that's allowed by the standards, but I tried, and it failed.

@Joris-van-der-Wel
Copy link

Client and server should be able to send a ping. If a pong reply is not sent, there is either a bug in this library or my example code (which I did not test 😈).

You could test this stuff with wireshark. Look here to figure out what the frames look like: https://tools.ietf.org/html/rfc6455#section-5.2
So a ping frame should start with the byte 0x89, a pong frame should start with the byte 0x8A.

@ghost
Copy link

ghost commented Feb 3, 2014

Interesting.

I'm not up to all of your levels to be able to diagnose this issue, so I'll merely report back my experience.

The ping works perfectly with the server, but with the client, it reports "control frames may no be fragmented" (thank goodness for the typo!). Google leads to this line of code: https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/java/org/java_websocket/drafts/Draft_10.java#L316

If FIN is fin in the message then something may be off because with debug on, I can see that fin is true. It may be "true" instead of true. I can't be certain.

Either way, works for me since it's sufficient that only the servers send pings since only clients will be connected to them, lol.

Thank you for that code!

@Joris-van-der-Wel
Copy link

Could you try this instead:

void sendPing(WebSocket conn)
{
        FramedataImpl1 frame = new FramedataImpl1(Opcode.PING);
        frame.setFin(true);
        conn.sendFrame(frame);
}

I had expected fin to be set by default, but it is not.

@marci4
Copy link
Collaborator

marci4 commented May 7, 2017

see #473

@marci4 marci4 closed this as completed May 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants