<div dir="ltr">I can verify that neither onError or onClose is being called however CloseTask is being called.<div><br></div><div>Robin<br><div><br></div><div><br></div><div><pre style="color:rgb(169,183,198);font-size:10.5pt;background-color:rgb(43,43,43)"><font face="monospace, monospace"><span style="color:rgb(204,120,50)">public class </span>WebSocketConnectionHandler <span style="color:rgb(204,120,50)">extends </span>AbstractReceiveListener <span style="color:rgb(204,120,50)">implements </span>WebSocketConnectionCallback {<br><br>  <span style="color:rgb(187,181,41)">@Override<br></span><span style="color:rgb(187,181,41)">  </span><span style="color:rgb(204,120,50)">public void </span><span style="color:rgb(255,198,109)">onConnect</span>(WebSocketHttpExchange exchange<span style="color:rgb(204,120,50)">, </span>WebSocketChannel webSocketChannel) {<br>    System.<span style="color:rgb(152,118,170);font-style:italic">out</span>.println(<span style="color:rgb(106,135,89)">&quot;onConnect&quot;</span>)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(204,120,50)"><br></span><span style="color:rgb(204,120,50)">    </span>webSocketChannel.getReceiveSetter().set(<span style="color:rgb(204,120,50)">this</span>)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)">    </span>webSocketChannel.resumeReceives()<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)">    </span>webSocketChannel.addCloseTask(channel -&gt; {<br>      System.<span style="color:rgb(152,118,170);font-style:italic">out</span>.println(<span style="color:rgb(106,135,89)">&quot;Closing&quot;</span>)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(204,120,50)"><br></span><span style="color:rgb(204,120,50)">    </span>})<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)">  </span>}<br><br>  <span style="color:rgb(187,181,41)">@Override<br></span><span style="color:rgb(187,181,41)">  </span><span style="color:rgb(204,120,50)">protected void </span><span style="color:rgb(255,198,109)">onFullTextMessage</span>(WebSocketChannel channel<span style="color:rgb(204,120,50)">, </span>BufferedTextMessage message) {<br>    System.<span style="color:rgb(152,118,170);font-style:italic">out</span>.println(<span style="color:rgb(106,135,89)">&quot;onFullTextMessage&quot;</span>)<span style="color:rgb(204,120,50)">;
</span><span style="font-size:10.5pt;color:rgb(204,120,50)">    super</span><span style="font-size:10.5pt">.onFullTexMessage(channel, message</span><span style="font-size:10.5pt">)</span><span style="font-size:10.5pt;color:rgb(204,120,50)">;</span><span style="color:rgb(204,120,50)"><br></span><span style="color:rgb(204,120,50)">  </span>}<br><br>  <span style="color:rgb(187,181,41)">@Override<br></span><span style="color:rgb(187,181,41)">  </span><span style="color:rgb(204,120,50)">protected void </span><span style="color:rgb(255,198,109)">onError</span>(WebSocketChannel channel<span style="color:rgb(204,120,50)">, </span>Throwable error) {<br>    System.<span style="color:rgb(152,118,170);font-style:italic">out</span>.println(<span style="color:rgb(106,135,89)">&quot;error&quot;</span>)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)">    super</span>.onError(channel<span style="color:rgb(204,120,50)">, </span>error)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)">  </span>}<br><br>  <span style="color:rgb(187,181,41)">@Override<br></span><span style="color:rgb(187,181,41)">  </span><span style="color:rgb(204,120,50)">protected void </span><span style="color:rgb(255,198,109)">onClose</span>(WebSocketChannel webSocketChannel<span style="color:rgb(204,120,50)">, </span>StreamSourceFrameChannel channel)<br>      <span style="color:rgb(204,120,50)">throws </span>IOException {<br>    System.<span style="color:rgb(152,118,170);font-style:italic">out</span>.println(<span style="color:rgb(106,135,89)">&quot;onClose&quot;</span>)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(204,120,50)"><br></span><span style="color:rgb(204,120,50)">    super</span>.onClose(webSocketChannel<span style="color:rgb(204,120,50)">, </span>channel)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)">  </span>}<br><br>  <span style="color:rgb(187,181,41)">@Override<br></span><span style="color:rgb(187,181,41)">  </span><span style="color:rgb(204,120,50)">protected void </span><span style="color:rgb(255,198,109)">onFullCloseMessage</span>(WebSocketChannel channel<span style="color:rgb(204,120,50)">, </span>BufferedBinaryMessage message)<br>      <span style="color:rgb(204,120,50)">throws </span>IOException {<br>    System.<span style="color:rgb(152,118,170);font-style:italic">out</span>.println(<span style="color:rgb(106,135,89)">&quot;onFullCloseMessage&quot;</span>)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(204,120,50)"><br></span><span style="color:rgb(204,120,50)">    super</span>.onFullCloseMessage(channel<span style="color:rgb(204,120,50)">, </span>message)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)">  </span>}<br><br>  <span style="color:rgb(187,181,41)">@Override<br></span><span style="color:rgb(187,181,41)">  </span><span style="color:rgb(204,120,50)">protected void </span><span style="color:rgb(255,198,109)">onCloseMessage</span>(CloseMessage cm<span style="color:rgb(204,120,50)">, </span>WebSocketChannel channel) {<br>    System.<span style="color:rgb(152,118,170);font-style:italic">out</span>.println(<span style="color:rgb(106,135,89)">&quot;onCloseMessage&quot;</span>)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(204,120,50)"><br></span><span style="color:rgb(204,120,50)">    super</span>.onCloseMessage(cm<span style="color:rgb(204,120,50)">, </span>channel)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)">  </span>}<br>}</font></pre></div><div><br></div></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div><span style="color:rgb(85,85,85);font-family:sans-serif;font-size:13px;line-height:19px;background-color:rgb(255,255,255);border-width:2px 0px 0px;border-style:solid;border-color:rgb(51,105,232);padding-top:2px;margin-top:2px"> Robin Anil |</span><span style="color:rgb(85,85,85);font-family:sans-serif;font-size:13px;line-height:19px;background-color:rgb(255,255,255);border-width:2px 0px 0px;border-style:solid;border-color:rgb(238,178,17);padding-top:2px;margin-top:2px"> Software Engineer</span></div></div></div></div>
<br><div class="gmail_quote">On Sun, Nov 22, 2015 at 3:07 PM, Stuart Douglas <span dir="ltr">&lt;<a href="mailto:sdouglas@redhat.com" target="_blank">sdouglas@redhat.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">@OnError should be called if @OnClose is not called (if neither is being called this is a bug).<br>
<br>
Stuart<br>
<span class=""><br>
----- Original Message -----<br>
&gt; From: &quot;Dennis Gesker&quot; &lt;<a href="mailto:dennis@gesker.com">dennis@gesker.com</a>&gt;<br>
&gt; To: &quot;Robin Anil&quot; &lt;<a href="mailto:robin.anil@gmail.com">robin.anil@gmail.com</a>&gt;<br>
&gt; Cc: <a href="mailto:undertow-dev@lists.jboss.org">undertow-dev@lists.jboss.org</a><br>
&gt; Sent: Saturday, 21 November, 2015 3:51:31 AM<br>
&gt; Subject: Re: [undertow-dev] Correctly shutting down a websocket handler<br>
&gt;<br>
&gt; Hi Robin:<br>
&gt;<br>
&gt; I&#39;m new to WebSockets but I&#39;m glad to share what has been working for me. My<br>
&gt; use cases are currently via Java SE not the Web so this may not apply. I&#39;m<br>
&gt; migrating a bunch of data from an old system and really just pounding on<br>
&gt; Undertow to see how it will behave when I&#39;m ready to build my production<br>
&gt; application. And, I in no way claim this is a best practice. But, maybe you<br>
&gt; will find it useful.<br>
&gt;<br>
&gt; Anyway, I add three additional methods to each of my deocorated websockets<br>
&gt; (clients) two of which I call from other objects as needed. With this<br>
&gt; approach I&#39;m not seeing any data loss. Seem to be getting timely responses<br>
&gt; from the server side WebSocket. And, always get a nice clean [1000] close<br>
&gt; codes.<br>
&gt;<br>
&gt; setup()<br>
&gt; tearDown()<br>
&gt; sendWorktoServer()<br>
&gt;<br>
&gt;<br>
&gt; public void sendWorkToServer(Object obj) {<br>
</span>&gt; // <a href="http://logger.info" rel="noreferrer" target="_blank">logger.info</a> (this.getClass().getSimpleName() + &quot; &gt;&gt;&gt; Enter<br>
<span class="">&gt; sendWorkToServer&quot;);<br>
&gt;<br>
&gt; if (webSocketSession == null || !webSocketSession.isOpen()) {<br>
&gt; setUp();<br>
&gt; }<br>
&gt;<br>
&gt; if (obj == null || obj.getId() == null || obj.getId().isEmpty()) {<br>
&gt; return;<br>
&gt; }<br>
&gt;<br>
&gt; ObjectMapper mapper = new ObjectMapper();<br>
&gt; String json = null;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; try {<br>
&gt; json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);<br>
&gt; <a href="http://logger.info" rel="noreferrer" target="_blank">logger.info</a> (json);<br>
&gt; webSocketSession.getAsyncRemote().sendText(json);<br>
&gt; //messageLatch.await(500, TimeUnit.MILLISECONDS);<br>
&gt; } catch (JsonProcessingException e) {<br>
&gt; e.printStackTrace();<br>
&gt; }<br>
&gt;<br>
&gt;<br>
</span>&gt; <a href="http://logger.info" rel="noreferrer" target="_blank">logger.info</a> (&quot;Socket is open: &quot; + webSocketSession.isOpen());<br>
&gt; <a href="http://logger.info" rel="noreferrer" target="_blank">logger.info</a> (&quot;Exit sendWorkToServer&quot;);<br>
<span class="">&gt; }<br>
&gt;<br>
&gt; private boolean setUp() { //Called if needed inside sendWorkToServer() method<br>
&gt;<br>
&gt; try {<br>
&gt; websocketServerURI = serverSocketURI.getAddCfgStructureWsUri(); // Get<br>
&gt; Correct URI Development vs Production<br>
</span>&gt; <a href="http://logger.info" rel="noreferrer" target="_blank">logger.info</a> (&quot;URI: &quot; + websocketServerURI.toString());<br>
<div class="HOEnZb"><div class="h5">&gt; webSocketContainer = ContainerProvider.getWebSocketContainer(); // WebSocket<br>
&gt; Container<br>
&gt; webSocketSession = webSocketContainer.connectToServer(this.getClass(),<br>
&gt; websocketServerURI); // Connect to the Server<br>
&gt; if (!webSocketSession.isOpen()) {<br>
&gt; messageLatch.await(10, TimeUnit.SECONDS);<br>
&gt; }<br>
&gt;<br>
&gt; } catch (DeploymentException e) {<br>
&gt; e.printStackTrace();<br>
&gt; return false;<br>
&gt; } catch (IOException e) {<br>
&gt; e.printStackTrace();<br>
&gt; return false;<br>
&gt; } catch (InterruptedException e) {<br>
&gt; e.printStackTrace();<br>
&gt; }<br>
&gt;<br>
&gt; return true;<br>
&gt;<br>
&gt; }<br>
&gt;<br>
&gt; public boolean tearDown() {<br>
&gt;<br>
&gt; if (webSocketSession != null) {<br>
&gt; if (webSocketSession.isOpen()) {<br>
&gt; try {<br>
&gt; webSocketSession.close();<br>
&gt; } catch (IOException e) {<br>
&gt; e.printStackTrace();<br>
&gt; return false;<br>
&gt; }<br>
&gt; }<br>
&gt; }<br>
&gt; webSocketSession = null;<br>
&gt; webSocketContainer = null;<br>
&gt; websocketServerURI = null;<br>
&gt; serverSocketURI = null;<br>
&gt; return true;<br>
&gt; }<br>
&gt;<br>
&gt;<br>
&gt; So, inside my other object the code looks something like...<br>
&gt;<br>
&gt; WebSocketClient wcs = new WebSocketClient();<br>
&gt; wcs.sendWorkToServer(Object obj);<br>
&gt; // More work and activities<br>
&gt; wcs.teardown();<br>
&gt; wcs = null;<br>
&gt;<br>
&gt; Also, what might be helpful is to add a custom method that just return the<br>
&gt; status of your websocketclient session and then call teardown() as needed.<br>
&gt; I hope that helps a bit... In my scenario I take some extra steps to control<br>
&gt; the life cycle so I don&#39;t really have to detect status.<br>
&gt;<br>
&gt; Cordially,<br>
&gt; Dennis<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; On Thu, Nov 19, 2015 at 1:12 PM, Robin Anil &lt; <a href="mailto:robin.anil@gmail.com">robin.anil@gmail.com</a> &gt; wrote:<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; When a client disconnects, I see that onClose is not being fired. The only<br>
&gt; way this seems to be firing if client sents a close frame.<br>
&gt;<br>
&gt; Is there any way to detect disconnection and immediately close all the opened<br>
&gt; resources.<br>
&gt;<br>
&gt; Robin<br>
&gt;<br>
&gt; Robin Anil | Software Engineer<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; undertow-dev mailing list<br>
&gt; <a href="mailto:undertow-dev@lists.jboss.org">undertow-dev@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/undertow-dev" rel="noreferrer" target="_blank">https://lists.jboss.org/mailman/listinfo/undertow-dev</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt;<br>
</div></div><span class="im HOEnZb">&gt; “Be without fear in the face of your enemies. Be brave and upright that God<br>
&gt; may love thee. Speak the truth always, even if it leads to your death.<br>
&gt; Safeguard the helpless and do no wrong – that is your oath.” -The Knight’s<br>
&gt; Oath (Kingdom of Heaven)<br>
&gt;<br>
</span><div class="HOEnZb"><div class="h5">&gt; _______________________________________________<br>
&gt; undertow-dev mailing list<br>
&gt; <a href="mailto:undertow-dev@lists.jboss.org">undertow-dev@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/undertow-dev" rel="noreferrer" target="_blank">https://lists.jboss.org/mailman/listinfo/undertow-dev</a><br>
</div></div></blockquote></div><br></div>