WebSocketJson.java
2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package net.ziemers.swxercise.ui.ws;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonReaderFactory;
import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;
import java.io.StringReader;
import java.util.Collections;
/**
* Ein Beispiel-JSON zur Übertragung durch den WebSocket.
*/
public class WebSocketJson {
/**
* Wandelt ein Objekt der Klasse in einen JSON-String um.
*/
public static class MessageEncoder implements Encoder.Text<WebSocketJson> {
@Override
public void init(EndpointConfig config) {}
@Override
public String encode(WebSocketJson message) throws EncodeException {
return Json.createObjectBuilder()
.add("message", message.getMessage())
.build()
.toString();
}
@Override
public void destroy() {}
}
/**
* Wandelt einen JSON-String in ein Objekt der Klasse um.
*/
public static class MessageDecoder implements Decoder.Text<WebSocketJson> {
private JsonReaderFactory factory = Json.createReaderFactory(Collections.emptyMap());
@Override
public void init(EndpointConfig config) {}
@Override
public WebSocketJson decode(String str) throws DecodeException {
WebSocketJson message = new WebSocketJson();
JsonReader reader = factory.createReader(new StringReader(str));
JsonObject json = reader.readObject();
message.setMessage(json.getString("message"));
return message;
}
@Override
public boolean willDecode(String str) {
return true;
}
@Override
public void destroy() {}
}
/*
* Payload der JSON-Nachricht
*/
private String message;
private WebSocketJson() {}
public String getMessage() {
return message;
}
private void setMessage(String message) {
this.message = message;
}
}