[undertow-dev] Correctly shutting down a websocket handler

Robin Anil robin.anil at gmail.com
Mon Nov 23 14:37:01 EST 2015


I can verify that neither onError or onClose is being called however
CloseTask is being called.

Robin


public class WebSocketConnectionHandler extends
AbstractReceiveListener implements WebSocketConnectionCallback {

  @Override
  public void onConnect(WebSocketHttpExchange exchange,
WebSocketChannel webSocketChannel) {
    System.out.println("onConnect");
    webSocketChannel.getReceiveSetter().set(this);
    webSocketChannel.resumeReceives();
    webSocketChannel.addCloseTask(channel -> {
      System.out.println("Closing");
    });
  }

  @Override
  protected void onFullTextMessage(WebSocketChannel channel,
BufferedTextMessage message) {
    System.out.println("onFullTextMessage");
super.onFullTexMessage(channel, message);
  }

  @Override
  protected void onError(WebSocketChannel channel, Throwable error) {
    System.out.println("error");
    super.onError(channel, error);
  }

  @Override
  protected void onClose(WebSocketChannel webSocketChannel,
StreamSourceFrameChannel channel)
      throws IOException {
    System.out.println("onClose");
    super.onClose(webSocketChannel, channel);
  }

  @Override
  protected void onFullCloseMessage(WebSocketChannel channel,
BufferedBinaryMessage message)
      throws IOException {
    System.out.println("onFullCloseMessage");
    super.onFullCloseMessage(channel, message);
  }

  @Override
  protected void onCloseMessage(CloseMessage cm, WebSocketChannel channel) {
    System.out.println("onCloseMessage");
    super.onCloseMessage(cm, channel);
  }
}



 Robin Anil | Software Engineer

On Sun, Nov 22, 2015 at 3:07 PM, Stuart Douglas <sdouglas at redhat.com> wrote:

> @OnError should be called if @OnClose is not called (if neither is being
> called this is a bug).
>
> Stuart
>
> ----- Original Message -----
> > From: "Dennis Gesker" <dennis at gesker.com>
> > To: "Robin Anil" <robin.anil at gmail.com>
> > Cc: undertow-dev at lists.jboss.org
> > Sent: Saturday, 21 November, 2015 3:51:31 AM
> > Subject: Re: [undertow-dev] Correctly shutting down a websocket handler
> >
> > Hi Robin:
> >
> > I'm new to WebSockets but I'm glad to share what has been working for
> me. My
> > use cases are currently via Java SE not the Web so this may not apply.
> I'm
> > migrating a bunch of data from an old system and really just pounding on
> > Undertow to see how it will behave when I'm ready to build my production
> > application. And, I in no way claim this is a best practice. But, maybe
> you
> > will find it useful.
> >
> > Anyway, I add three additional methods to each of my deocorated
> websockets
> > (clients) two of which I call from other objects as needed. With this
> > approach I'm not seeing any data loss. Seem to be getting timely
> responses
> > from the server side WebSocket. And, always get a nice clean [1000] close
> > codes.
> >
> > setup()
> > tearDown()
> > sendWorktoServer()
> >
> >
> > public void sendWorkToServer(Object obj) {
> > // logger.info (this.getClass().getSimpleName() + " >>> Enter
> > sendWorkToServer");
> >
> > if (webSocketSession == null || !webSocketSession.isOpen()) {
> > setUp();
> > }
> >
> > if (obj == null || obj.getId() == null || obj.getId().isEmpty()) {
> > return;
> > }
> >
> > ObjectMapper mapper = new ObjectMapper();
> > String json = null;
> >
> >
> >
> > try {
> > json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
> > logger.info (json);
> > webSocketSession.getAsyncRemote().sendText(json);
> > //messageLatch.await(500, TimeUnit.MILLISECONDS);
> > } catch (JsonProcessingException e) {
> > e.printStackTrace();
> > }
> >
> >
> > logger.info ("Socket is open: " + webSocketSession.isOpen());
> > logger.info ("Exit sendWorkToServer");
> > }
> >
> > private boolean setUp() { //Called if needed inside sendWorkToServer()
> method
> >
> > try {
> > websocketServerURI = serverSocketURI.getAddCfgStructureWsUri(); // Get
> > Correct URI Development vs Production
> > logger.info ("URI: " + websocketServerURI.toString());
> > webSocketContainer = ContainerProvider.getWebSocketContainer(); //
> WebSocket
> > Container
> > webSocketSession = webSocketContainer.connectToServer(this.getClass(),
> > websocketServerURI); // Connect to the Server
> > if (!webSocketSession.isOpen()) {
> > messageLatch.await(10, TimeUnit.SECONDS);
> > }
> >
> > } catch (DeploymentException e) {
> > e.printStackTrace();
> > return false;
> > } catch (IOException e) {
> > e.printStackTrace();
> > return false;
> > } catch (InterruptedException e) {
> > e.printStackTrace();
> > }
> >
> > return true;
> >
> > }
> >
> > public boolean tearDown() {
> >
> > if (webSocketSession != null) {
> > if (webSocketSession.isOpen()) {
> > try {
> > webSocketSession.close();
> > } catch (IOException e) {
> > e.printStackTrace();
> > return false;
> > }
> > }
> > }
> > webSocketSession = null;
> > webSocketContainer = null;
> > websocketServerURI = null;
> > serverSocketURI = null;
> > return true;
> > }
> >
> >
> > So, inside my other object the code looks something like...
> >
> > WebSocketClient wcs = new WebSocketClient();
> > wcs.sendWorkToServer(Object obj);
> > // More work and activities
> > wcs.teardown();
> > wcs = null;
> >
> > Also, what might be helpful is to add a custom method that just return
> the
> > status of your websocketclient session and then call teardown() as
> needed.
> > I hope that helps a bit... In my scenario I take some extra steps to
> control
> > the life cycle so I don't really have to detect status.
> >
> > Cordially,
> > Dennis
> >
> >
> >
> >
> >
> >
> >
> > On Thu, Nov 19, 2015 at 1:12 PM, Robin Anil < robin.anil at gmail.com >
> wrote:
> >
> >
> >
> > When a client disconnects, I see that onClose is not being fired. The
> only
> > way this seems to be firing if client sents a close frame.
> >
> > Is there any way to detect disconnection and immediately close all the
> opened
> > resources.
> >
> > Robin
> >
> > Robin Anil | Software Engineer
> >
> > _______________________________________________
> > undertow-dev mailing list
> > undertow-dev at lists.jboss.org
> > https://lists.jboss.org/mailman/listinfo/undertow-dev
> >
> >
> >
> > --
> >
> > “Be without fear in the face of your enemies. Be brave and upright that
> God
> > may love thee. Speak the truth always, even if it leads to your death.
> > Safeguard the helpless and do no wrong – that is your oath.” -The
> Knight’s
> > Oath (Kingdom of Heaven)
> >
> > _______________________________________________
> > undertow-dev mailing list
> > undertow-dev at lists.jboss.org
> > https://lists.jboss.org/mailman/listinfo/undertow-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/undertow-dev/attachments/20151123/7f81735c/attachment.html 


More information about the undertow-dev mailing list