I am implementing a small chat application using java-ee websockets.
At one time I want to close session for a client for various reasons so for closing connection.
For closing connection I called onClose
function and in that function I called session.close()
But after that I got the following error:
java.lang.IllegalStateException: The WebSocket session has been closed and no method (apart from close()) may be called on a closed session
at org.apache.tomcat.websocket.WsSession.checkState(WsSession.java:643)
at org.apache.tomcat.websocket.WsSession.addMessageHandler(WsSession.java:168)
at org.apache.tomcat.websocket.pojo.PojoEndpointBase.doOnOpen(PojoEndpointBase.java:81)
at org.apache.tomcat.websocket.pojo.PojoEndpointServer.onOpen(PojoEndpointServer.java:70)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:129)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:629)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
I'm not sure what I did wrong and I why I got this exception.
I was having the same issue as I started learning this stuff today.
What I have found so far is that on the server at the annotation @OnMessage I was closing the connection when the Server received a "quit" message by calling the session.close. Then at the end of the @OnMessage I was issuing the return that is mandatory as the method requires a return and it was the return that was throwing the Exception since the session has been closed already.
My solution is to instead of making the server close the session I send a message "youquit" back to the client and I have the client calling the session.close.
The server now receives the close event and is ready for the next connection and the client ends without exception.
@OnMessage
public String onMessage(String message, Session session) throws DeploymentException, IOException {
switch (message) {
case "quit":
//session.close(new CloseReason(CloseCodes.NORMAL_CLOSURE, "Game ended"));
message = "you" + message;
break;
}
return message;
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
logger.info(String.format("Session %s closed because of %s", session.getId(), closeReason));
}
I have tried to catch the exception at the return level but it does not work.
I now believe that you cannot close the connection at the @OnMessage entry point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With