title: Jetty Websockets - Correctly sending async messages when handling unreliable connections
body: <p>I'm using Jetty 9.3.5 and I would like to know what is the proper way to handle unreliable connections when sending websocket messages, specifically: I noticed cases when a websocket connection does not close normally so, even though the client side is down, it takes a lot of time until onClose() is triggered on the server (for ex. a user closes the laptop lid and puts it in standby - it can take 1-2 hours until the close event is received on the server side). </p>
  
  <p>Thus, because the client is still registered, the server keeps sending messages that begin to build up. This becomes an issue when sending a large number of messages.</p>
  
  <p>I've tested sending byte messages with:</p>
  
  <pre><code>Session.getRemote().sendBytes(ByteBuffer, WriteCallback)
  Session.getRemote().sendBytesByFuture(ByteBuffer);
  </code></pre>
  
  <p>To simulate the connection down on one side (ie. user puts laptop in standby), on Linux, I assigned an IP address to eth0 interface, started sending the messages and then brought it down:</p>
  
  <pre><code>ifconfig eth0 192.168.1.1
  ifconfig eth0 up
  --- start sending messages (simple incremented numbers) and connect using Chrome browser and print them ---
  ifconfig eth0 down
  </code></pre>
  
  <p>This way: the messages were still being sent by Jetty, the Chrome client did not receive them, the onCllose or onError was not triggered on server-side</p>
  
  <p>My questions regarding Jetty are:</p>
  
  <ol>
  <li><p>Is there a way to clear queued messages that were not delivered?
  I've tried, but with no luck:</p>
  
  <p>Session.getRemote().flush();</p></li>
  <li><p>Can a max number of queued messages be set?
  I've tried:</p>
  
  <p>WebSocketServletFactory.getPolicy().setMaxBinaryMessageBufferSize(1)</p></li>
  <li><p>Can I detect if the client does not receive the message? (or if the connection is in abnormal state let's say)
  I've tried: </p>
  
  <pre><code>session.getRemote().sendBytes(bb, new WriteCallback() {
  
                  @Override
                  public void writeSuccess() {
                      //print success                     }
  
                  @Override
                  public void writeFailed(Throwable arg0) {
                      //print fail
                  }
              });
  </code></pre></li>
  </ol>
  
  <p>But this prints success even though the messages are not received.</p>
  
  <p>I also tried to use, but couldn't find a solution:</p>
  
  <pre><code>factory.getPolicy().setIdleTimeout(...);
  factory.getPolicy().setAsyncWriteTimeout(3000);
  sendPing()
  </code></pre>
  
  <p>Thanks in advance!</p>