Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
Adding ping-pong. Fixes #71
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines committed Oct 21, 2018
1 parent bed64d4 commit fd64442
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
33 changes: 29 additions & 4 deletions service/src/main/java/com/simplevote/webservice/PollWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import ch.qos.logback.classic.Logger;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.simplevote.db.Actions;
Expand All @@ -20,7 +19,6 @@
import org.javalite.activejdbc.LazyList;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -36,10 +34,12 @@ public class PollWebSocket {
// A map with the user to their poll id
private static Map<User, Long> userPollMap = new HashMap<>();

private static final Integer PING_DELAY = 10000;

enum MessageType {
poll, pollComments, pollUsers, pollActiveUsers, pollQuestions, pollCandidates, pollVotes, updatePoll, deletePoll,
createComment, deleteComment, createQuestion, deleteQuestion, updateQuestion, createCandidate, deleteCandidate,
updateCandidate, createOrUpdateVote, deleteVote;
updateCandidate, createOrUpdateVote, deleteVote, Ping, Pong;
}

@OnWebSocketConnect
Expand All @@ -50,6 +50,8 @@ public void onConnect(Session session) throws Exception {
Long pollId = getPollIdFromSession(session);
sessionPollMap.put(session, pollId);

sendRecurringPings(session);

// Send the poll
sendMessage(session, messageWrapper(MessageType.poll, Actions.getPoll(pollId).toJson(false)));

Expand Down Expand Up @@ -127,7 +129,9 @@ public void onMessage(Session session, String dataStr) {
case deleteVote:
deleteVote(session, data);
break;

case Pong:
pongReceived(session, data);
break;
}

} catch (Exception e) {
Expand Down Expand Up @@ -296,6 +300,27 @@ public void deleteVote(Session session, JsonNode data) {

}

private void sendRecurringPings(Session session) {
final Timer timer = new Timer();
final TimerTask tt = new TimerTask() {
@Override
public void run() {
if (session.isOpen()) {
sendMessage(session, messageWrapper(MessageType.Ping, "{}"));
} else {
timer.cancel();
timer.purge();
}
}
};

timer.scheduleAtFixedRate(tt, PING_DELAY, PING_DELAY);
}

private void pongReceived(Session session, JsonNode data) {
log.debug("Pong received from " + session.getRemoteAddress());
}

private static String messageWrapper(MessageType type, String data) {
return "{\"message_type\":" + type.ordinal() + ",\"data\":" + data + "}";
}
Expand Down
13 changes: 10 additions & 3 deletions ui/src/app/components/poll/poll.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class PollComponent implements OnInit {
public isLoading: boolean = false;
private initEditing: boolean = false;

private reconnnectTimeWaitMS: number = 60000;
private reconnnectTimeWaitMS: number = 10000;

@ViewChild('reconnectModal') private reconnectModal: ModalDirective;

Expand Down Expand Up @@ -188,8 +188,9 @@ export class PollComponent implements OnInit {
case MessageType.deleteVote:
this.receiveDeleteVote(msg.data);
break;


case MessageType.Ping:
this.sendPong();
break;
default:
alert('wrong message: ' + dataStr);
}
Expand Down Expand Up @@ -468,6 +469,12 @@ export class PollComponent implements OnInit {
this.poll.comments.splice(commentIndex, 1);
}

sendPong() {
console.debug("Received ping, sending pong");
this.pollService.send(Tools.messageWrapper(MessageType.Pong,
{}));
}

exportPoll() {
let pollJson: string = JSON.stringify(this.poll, null, 2);

Expand Down
3 changes: 2 additions & 1 deletion ui/src/app/shared/message-type.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export enum MessageType {
createComment, deleteComment,
createQuestion, deleteQuestion, updateQuestion,
createCandidate, deleteCandidate, updateCandidate,
createOrUpdateVote, deleteVote
createOrUpdateVote, deleteVote,
Ping, Pong
}

0 comments on commit fd64442

Please sign in to comment.