[undertow-dev] Correctly shutting down a websocket handler

Dennis Gesker dennis at gesker.com
Fri Nov 20 11:51:31 EST 2015


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
>



-- 
[image: LinkedIn] <https://www.linkedin.com/in/gesker>
<https://www.facebook.com/gesker>[image: Wordpress]
<https://gesker.wordpress.com/>[image: Facebook][image: Twitter]
<https://twitter.com/gesker> <https://gesker.wordpress.com/>
<http://www.gesker.com> <https://google.com/+DennisGesker>[image: Family
Home Page][image: Public Encryption Key]
<https://pgp.mit.edu/pks/lookup?search=dennis%40gesker.com&op=index>[image:
dennis at gesker.com] <dennis at gesker.com>
“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)*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/undertow-dev/attachments/20151120/6d84940d/attachment-0001.html 


More information about the undertow-dev mailing list