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

feat: Implement first simple WebSocket, so far no connection

parent 020a8162
No related branches found
No related tags found
No related merge requests found
......@@ -165,6 +165,20 @@
<version>1.3.4</version>
</dependency>
<!-- websocket api -->
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
</dependency>
<!-- gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark</artifactId>
......
package unipotsdam.gf.modules.annotation.model;
public class AnnotationMessage {
// variables
private String from;
private AnnotationMessageType type;
private String annotationId;
public enum AnnotationMessageType {
GET,
DELETE
}
// methods
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public AnnotationMessageType getType() {
return type;
}
public void setType(AnnotationMessageType type) {
this.type = type;
}
public String getAnnotationId() {
return annotationId;
}
public void setAnnotationId(String annotationId) {
this.annotationId = annotationId;
}
@Override
public String toString() {
return "AnnotationMessage{" +
"from='" + from + '\'' +
", type=" + type +
", annotationId='" + annotationId + '\'' +
'}';
}
}
package unipotsdam.gf.modules.annotation.websocket;
import com.google.gson.Gson;
import unipotsdam.gf.modules.annotation.model.AnnotationMessage;
import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;
public class AnnotationMessageDecoder implements Decoder.Text<AnnotationMessage> {
public static Gson gson = new Gson();
@Override
public AnnotationMessage decode(String s) throws DecodeException {
AnnotationMessage annotationMessage = gson.fromJson(s, AnnotationMessage.class);
return annotationMessage;
}
@Override
public boolean willDecode(String s) {
return (null != s);
}
@Override
public void init(EndpointConfig endpointConfig) {
// todo
}
@Override
public void destroy() {
// todo
}
}
package unipotsdam.gf.modules.annotation.websocket;
import com.google.gson.Gson;
import unipotsdam.gf.modules.annotation.model.AnnotationMessage;
import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;
public class AnnotationMessageEncoder implements Encoder.Text<AnnotationMessage> {
private static Gson gson = new Gson();
@Override
public String encode(AnnotationMessage annotationMessage) throws EncodeException {
String json = gson.toJson(annotationMessage);
return json;
}
@Override
public void init(EndpointConfig endpointConfig) {
// todo
}
@Override
public void destroy() {
// todo
}
}
package unipotsdam.gf.modules.annotation.websocket;
import unipotsdam.gf.modules.annotation.model.AnnotationMessage;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
@ServerEndpoint(value = "/ws/annotation/{targetId}", decoders = AnnotationMessageDecoder.class, encoders = AnnotationMessageEncoder.class)
public class AnnotationWebSocketEndpoint {
private Session session;
private static final Set<AnnotationWebSocketEndpoint> endpoints = new CopyOnWriteArraySet<>();
private static HashMap<String, String> targets = new HashMap<>();
@OnOpen
public void onOpen(Session session, @PathParam("targetId") String targetId) throws IOException {
// initialize session
this.session = session;
// save endpoint in set of endpoints
endpoints.add(this);
// save mapping of session and target id
targets.put(session.getId(), targetId);
System.out.println(endpoints.toString());
}
@OnMessage
public void onMessage(Session session, AnnotationMessage annotationMessage) throws IOException, EncodeException {
annotationMessage.setFrom(targets.get(session.getId()));
broadcast(annotationMessage);
}
@OnClose
public void onClose(Session session) throws IOException {
endpoints.remove(this);
}
@OnError
public void onError(Session session, Throwable throwable) {
// todo
}
private void broadcast(AnnotationMessage annotationMessage) throws IOException, EncodeException {
endpoints.forEach(endpoint -> {
synchronized (endpoint) {
try {
if (targets.get(endpoint.session.getId()).equals(annotationMessage.getFrom())) {
endpoint.session.getBasicRemote().sendObject(annotationMessage);
}
}
catch (IOException | EncodeException e) {
e.printStackTrace();
}
}
});
}
}
......@@ -12,6 +12,8 @@ var documentText, startCharacter, endCharacter;
*/
$(document).ready(function() {
connect("200");
/**
* Context menu handler
*/
......
var ws;
function connect(targetId) {
var host = document.location.host;
var pathname = document.location.pathname;
ws = new WebSocket("ws://" + host + "/ws/annotation/" + targetId);
ws.onmessage = function (e) {
var message = JSON.parse(e.data);
console.log(message.from)
};
}
function send(type, annotationId) {
var json = JSON.stringify({
"type":type,
"annotationId":annotationId
})
ws.send(json);
}
\ No newline at end of file
......@@ -34,6 +34,8 @@
<script src="https://swisnl.github.io/jQuery-contextMenu/dist/jquery.contextMenu.js" type="text/javascript"></script>
<!-- js - utility script -->
<script src="../assets/js/utility.js"></script>
<!-- js - annotation websocket script -->
<script src="../assets/js/annotationWebsocket.js"></script>
<!-- js - annotationScript -->
<script src="../assets/js/annotationScript.js"></script>
......
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