Skip to content
Snippets Groups Projects
Commit ae51cc81 authored by Sven Kästle's avatar Sven Kästle
Browse files

feat: Add targetId to AnnotationMessage and Implement WebSocket logic

parent 5e8da014
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@ package unipotsdam.gf.modules.annotation.model;
public class AnnotationMessage {
// variables
private String from;
private String targetId;
private AnnotationMessageType type;
private String annotationId;
......@@ -20,6 +21,14 @@ public class AnnotationMessage {
this.from = from;
}
public String getTargetId() {
return targetId;
}
public void setTargetId(String targetId) {
this.targetId = targetId;
}
public AnnotationMessageType getType() {
return type;
}
......@@ -40,6 +49,7 @@ public class AnnotationMessage {
public String toString() {
return "AnnotationMessage{" +
"from='" + from + '\'' +
", targetId='" + targetId + '\'' +
", type=" + type +
", annotationId='" + annotationId + '\'' +
'}';
......
......@@ -29,7 +29,8 @@ public class AnnotationWebSocketEndpoint {
@OnMessage
public void onMessage(Session session, AnnotationMessage annotationMessage) throws IOException, EncodeException {
annotationMessage.setFrom(targets.get(session.getId()));
annotationMessage.setTargetId(targets.get(session.getId()));
annotationMessage.setFrom(session.getId());
broadcast(annotationMessage);
}
......@@ -48,7 +49,9 @@ public class AnnotationWebSocketEndpoint {
endpoints.forEach(endpoint -> {
synchronized (endpoint) {
try {
if (targets.get(endpoint.session.getId()).equals(annotationMessage.getFrom())) {
if (targets.get(endpoint.session.getId()).equals(annotationMessage.getTargetId())
&& !endpoint.session.getId().equals(annotationMessage.getFrom())) {
System.out.println("Send message to session" + endpoint.session.getId() + " from session " + annotationMessage.getFrom());
endpoint.session.getBasicRemote().sendObject(annotationMessage);
}
}
......
......@@ -166,6 +166,8 @@ $(document).ready(function() {
// delte annotation from server and from list
deleteAnnotation(id, function () {
// send delete request to websocket
send("DELETE", id);
// remove annotation from list
$('#' + id).closest('.listelement').remove()
// remove highlighted text
......@@ -531,6 +533,8 @@ function saveNewAnnotation(title, comment, startCharacter, endCharacter) {
// send new annotation to back-end and display it in list
createAnnotation(annotationPostRequest, function(response) {
// send new annotation to websocket
send("GET", response.id);
// display the new annotation
displayAnnotation(response);
......
......@@ -9,6 +9,18 @@ function connect(targetId) {
ws.onmessage = function (e) {
var message = JSON.parse(e.data);
console.log(message.from)
if (message.type === "GET") {
// get annotation from server
getAnnotation(message.annotationId, function (response) {
// display annotation
displayAnnotation(response)
})
}
else if (message.type === "DELETE") {
// remove annotation from list
$('#' + message.annotationId).closest('.listelement').remove()
}
};
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment