From 5391c08a8a9ecd03c8c4e2b9333730e292f304fa Mon Sep 17 00:00:00 2001
From: Daniel Gerhardt <daniel.gerhardt@mni.thm.de>
Date: Sun, 25 May 2014 21:04:59 +0200
Subject: [PATCH] Improve active user count handling

---
 .../thm/arsnova/socket/ARSnovaSocketIOServer.java | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java
index 66d4a2aa0..63e5858ac 100644
--- a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java
+++ b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java
@@ -56,8 +56,6 @@ public class ARSnovaSocketIOServer {
 	private final Configuration config;
 	private SocketIOServer server;
 
-	private int lastActiveUserCount = 0;
-
 	public ARSnovaSocketIOServer() {
 		config = new Configuration();
 	}
@@ -112,6 +110,15 @@ public class ARSnovaSocketIOServer {
 		server.addEventListener("setSession", Session.class, new DataListener<Session>() {
 			@Override
 			public void onData(SocketIOClient client, Session session, AckRequest ackSender) {
+				User u = userService.getUser2SocketId(client.getSessionId());
+				String oldSessionKey = userService.getSessionForUser(u.getUsername());
+				if (session.getKeyword() == oldSessionKey) {
+					return;
+				}
+				if (null != oldSessionKey) {
+					reportActiveUserCountForSession(oldSessionKey);
+				}
+
 				if (null != sessionService.joinSession(session.getKeyword(), client.getSessionId())) {
 					/* active user count has to be sent to the client since the broadcast is
 					 * not always sent as long as the polling solution is active simultaneously */
@@ -282,10 +289,6 @@ public class ARSnovaSocketIOServer {
 	public void reportActiveUserCountForSession(String sessionKey) {
 		/* This check is needed as long as the HTTP polling solution is active simultaneously. */
 		int count = userService.getUsersInSessionCount(sessionKey);
-		if (count == lastActiveUserCount) {
-			return;
-		}
-		lastActiveUserCount = count;
 
 		broadcastInSession(sessionKey, "activeUserCountData", count);
 	}
-- 
GitLab