diff --git a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java index e07444860a912e470d9d6d5dc03396f302b5420d..456123f912b1a0c99f02ba2299caf083dafd12db 100644 --- a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java +++ b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java @@ -26,6 +26,7 @@ import de.thm.arsnova.entities.Question; import de.thm.arsnova.entities.User; import de.thm.arsnova.services.IFeedbackService; import de.thm.arsnova.services.IQuestionService; +import de.thm.arsnova.services.ISessionService; import de.thm.arsnova.services.IUserService; import de.thm.arsnova.socket.message.Feedback; import de.thm.arsnova.socket.message.Session; @@ -41,6 +42,9 @@ public class ARSnovaSocketIOServer { @Autowired private IUserService userService; + @Autowired + private ISessionService sessionService; + private final Logger logger = LoggerFactory.getLogger(getClass()); private int portNumber; @@ -98,6 +102,7 @@ public class ARSnovaSocketIOServer { @Override public void onData(SocketIOClient client, Session session, AckRequest ackSender) { userService.addUserToSessionBySocketId(client.getSessionId(), session.getKeyword()); + reportActiveUserCountForSession(session.getKeyword()); } }); @@ -119,6 +124,10 @@ public class ARSnovaSocketIOServer { @Override public void onDisconnect(SocketIOClient client) { logger.info("addDisconnectListener.onDisconnect: Client: {}", new Object[] { client }); + String sessionKey = userService.getSessionForUser( + userService.getUser2SocketId(client.getSessionId()).getUsername() + ); + reportActiveUserCountForSession(sessionKey); userService.removeUserFromSessionBySocketId(client.getSessionId()); userService.removeUser2SocketId(client.getSessionId()); } @@ -205,21 +214,32 @@ public class ARSnovaSocketIOServer { return result; } - public void reportUpdatedFeedbackForSession(String session) { + public void reportUpdatedFeedbackForSession(String sessionKey) { + de.thm.arsnova.entities.Feedback fb = feedbackService.getFeedback(sessionKey); + broadcastInSession(sessionKey, "updateFeedback", fb.getValues()); + } + + public void reportActiveUserCountForSession(String sessionKey) { + int count = sessionService.countActiveUsers(sessionKey); + broadcastInSession(sessionKey, "updateActiveUserCount", count); + } + + public void broadcastInSession(String sessionKey, String eventName, Object data) { + logger.info("Broadcasting " + eventName + " for session " + sessionKey + "."); + /** * collect a list of users which are in the current session iterate over * all connected clients and if send feedback, if user is in current * session */ - List<User> users = userService.getUsersInSession(session); - de.thm.arsnova.entities.Feedback fb = feedbackService.getFeedback(session); + List<User> users = userService.getUsersInSession(sessionKey); for (SocketIOClient c : server.getAllClients()) { User u = userService.getUser2SocketId(c.getSessionId()); if (u != null && users.contains(u)) { logger.info("sending out to client {}, username is: {}, current session is: {}", - new Object[] { c.getSessionId(), u.getUsername(), session }); - c.sendEvent("updateFeedback", fb.getValues()); + new Object[] { c.getSessionId(), u.getUsername(), sessionKey }); + c.sendEvent(eventName, data); } } }