Index: lams_tool_chat/src/java/org/lamsfoundation/lams/tool/chat/web/actions/LearningWebsocketServer.java =================================================================== RCS file: /usr/local/cvsroot/lams_tool_chat/src/java/org/lamsfoundation/lams/tool/chat/web/actions/Attic/LearningWebsocketServer.java,v diff -u -r1.1.2.2 -r1.1.2.3 --- lams_tool_chat/src/java/org/lamsfoundation/lams/tool/chat/web/actions/LearningWebsocketServer.java 29 Jan 2016 09:13:10 -0000 1.1.2.2 +++ lams_tool_chat/src/java/org/lamsfoundation/lams/tool/chat/web/actions/LearningWebsocketServer.java 12 Apr 2016 12:03:07 -0000 1.1.2.3 @@ -116,7 +116,7 @@ } /** - * Cheks for stale connections and feeds the opened ones with messages and roster. + * Feeds opened websockets with messages and roster. */ private void send(Long toolSessionId) { // update the timestamp @@ -131,22 +131,7 @@ String rosterString = null; // synchronize websockets as a new Learner entering chat could modify this collection synchronized (sessionWebsockets) { - Iterator websocketIterator = sessionWebsockets.iterator(); - while (websocketIterator.hasNext()) { - Websocket websocket = websocketIterator.next(); - - // check whether the connection is not stale - if (!websocket.session.isOpen()) { - // remove the closed connection - websocketIterator.remove(); - - if (LearningWebsocketServer.log.isDebugEnabled()) { - LearningWebsocketServer.log.debug( - "User " + websocket.userName + " left Chat with toolSessionId: " + toolSessionId); - } - continue; - } - + for (Websocket websocket : sessionWebsockets) { // the connection is valid, carry on JSONObject responseJSON = new JSONObject(); // fetch roster only once, but messages are personalised @@ -280,15 +265,33 @@ } /** - * If there was something wrong with the connection, put it into logs. + * When user leaves the activity. */ @OnClose - public void unregisterUser(CloseReason reason) { - if (!(reason.getCloseCode().equals(CloseCodes.GOING_AWAY) - || reason.getCloseCode().equals(CloseCodes.NORMAL_CLOSURE))) { - LearningWebsocketServer.log.warn("Abnormal Chat websocket close. Code: " + reason.getCloseCode() - + ". Reason: " + reason.getReasonPhrase()); + public void unregisterUser(Session session, CloseReason reason) { + Long toolSessionId = Long + .valueOf(session.getRequestParameterMap().get(AttributeNames.PARAM_TOOL_SESSION_ID).get(0)); + Set sessionWebsockets = LearningWebsocketServer.websockets.get(toolSessionId); + synchronized (sessionWebsockets) { + Iterator websocketIterator = sessionWebsockets.iterator(); + while (websocketIterator.hasNext()) { + Websocket websocket = websocketIterator.next(); + if (websocket.session.equals(session)) { + websocketIterator.remove(); + break; + } + } } + + if (LearningWebsocketServer.log.isDebugEnabled()) { + LearningWebsocketServer.log.debug( + "User " + session.getUserPrincipal().getName() + " left Chat with toolSessionId: " + toolSessionId + + (!(reason.getCloseCode().equals(CloseCodes.GOING_AWAY) + || reason.getCloseCode().equals(CloseCodes.NORMAL_CLOSURE)) + ? ". Abnormal close. Code: " + reason.getCloseCode() + ". Reason: " + + reason.getReasonPhrase() + : "")); + } } /**