@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(a)gesker.com>
To: "Robin Anil" <robin.anil(a)gmail.com>
Cc: undertow-dev(a)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(a)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(a)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(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/undertow-dev