Skip to content

Commit

Permalink
Added null check for run id before sending message via websocket. NPE…
Browse files Browse the repository at this point in the history
… will no longer be thrown when authors were connected via web socket and closed session. Fixes #898.
  • Loading branch information
hirokiterashima committed Nov 9, 2017
1 parent b0c85d5 commit 01addff
Showing 1 changed file with 42 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -357,22 +357,23 @@ private void handleUserDisconnecting(WISEWebSocketSession wiseWebSocketSession)
String userName = wiseWebSocketSession.getUserName();
Long runId = wiseWebSocketSession.getRunId();

// add the user name and run id
messageJSON.put("userName", userName);
messageJSON.put("runId", runId);

String message = messageJSON.toString(); // get the message as a string
if (runId != null) {
// add the user name and run id
messageJSON.put("userName", userName);
messageJSON.put("runId", runId);

// get all the teachers that are currently connected for this run
Set<WISEWebSocketSession> teacherConnectionsForRun = getTeacherConnectionsForRun(runId);
String message = messageJSON.toString(); // get the message as a string

// send the message to all the teachers currently connected for this run
sendMessageToConnections(message, teacherConnectionsForRun);
// get all the teachers that are currently connected for this run
Set<WISEWebSocketSession> teacherConnectionsForRun = getTeacherConnectionsForRun(runId);

// also send updated studentsOnline list to all the teacher currently connected for this run
JSONObject studentsOnlineMessage = createStudentsOnlineMessage(wiseWebSocketSession);
sendMessageToConnections(studentsOnlineMessage.toString(), teacherConnectionsForRun);
// send the message to all the teachers currently connected for this run
sendMessageToConnections(message, teacherConnectionsForRun);

// also send updated studentsOnline list to all the teacher currently connected for this run
JSONObject studentsOnlineMessage = createStudentsOnlineMessage(wiseWebSocketSession);
sendMessageToConnections(studentsOnlineMessage.toString(), teacherConnectionsForRun);
}
} catch (JSONException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -497,37 +498,39 @@ private void addSession(WISEWebSocketSession wiseWebSocketSession) {
* @param wiseWebSocketSession the WISEWebSocketSession to remove from our collections
*/
private void removeSession(WISEWebSocketSession wiseWebSocketSession) {
removeWebSocketSessionFromRunConnections(wiseWebSocketSession);
removeWebSocketSessionfromSessions(wiseWebSocketSession);
removeWebSocketSessionFromUsers(wiseWebSocketSession);
}

if (wiseWebSocketSession != null) {

WebSocketSession session = wiseWebSocketSession.getSession(); // get the session

User user = wiseWebSocketSession.getUser(); //get the user

if (session != null) {

Long runId = getValueFromSession(session, "runId"); //get the run id

if (wiseWebSocketSession.isTeacher()) {
// user is a teacher so we will get the set of teacher connections for the run
Set<WISEWebSocketSession> teacherConnectionsForRun = runToTeacherConnections.get(runId);
private void removeWebSocketSessionFromUsers(WISEWebSocketSession wiseWebSocketSession) {
User user = wiseWebSocketSession.getUser();
userToWISEWebSocketSession.remove(user);
}

if (teacherConnectionsForRun != null) {
// remove the teacher session from the set of teacher connections for the run
teacherConnectionsForRun.remove(wiseWebSocketSession);
}
} else {
// user is a student so we will get the set of student connections for the run
Set<WISEWebSocketSession> studentConnectionsForRun = runToStudentConnections.get(runId);
private void removeWebSocketSessionfromSessions(WISEWebSocketSession wiseWebSocketSession) {
WebSocketSession session = wiseWebSocketSession.getSession();
sessionToWISEWebSocketSession.remove(session);
}

if (studentConnectionsForRun != null) {
// remove the student session from the set of student connections for the run
studentConnectionsForRun.remove(wiseWebSocketSession);
}
private void removeWebSocketSessionFromRunConnections(WISEWebSocketSession wiseWebSocketSession) {
WebSocketSession session = wiseWebSocketSession.getSession();
Long runId = getValueFromSession(session, "runId");
if (runId != null) {
if (wiseWebSocketSession.isTeacher()) {
// user is a teacher so we will get the set of teacher connections for the run
Set<WISEWebSocketSession> teacherConnectionsForRun = runToTeacherConnections.get(runId);
if (teacherConnectionsForRun != null) {
// remove the teacher session from the set of teacher connections for the run
teacherConnectionsForRun.remove(wiseWebSocketSession);
}
} else {
// user is a student so we will get the set of student connections for the run
Set<WISEWebSocketSession> studentConnectionsForRun = runToStudentConnections.get(runId);
if (studentConnectionsForRun != null) {
// remove the student session from the set of student connections for the run
studentConnectionsForRun.remove(wiseWebSocketSession);
}

sessionToWISEWebSocketSession.remove(session);
userToWISEWebSocketSession.remove(user);
}
}
}
Expand Down

0 comments on commit 01addff

Please sign in to comment.