Commit 9144b122220606f6b6223f7375b95542ee49de6f
1 parent
975ad860
feature: WebSockets um Logging und Parameter erweitert
Showing
2 changed files
with
43 additions
and
14 deletions
src/main/java/net/ziemers/swxercise/ui/ws/WebSocketController.java
1 | 1 | package net.ziemers.swxercise.ui.ws; |
2 | 2 | |
3 | -import javax.enterprise.context.ApplicationScoped; | |
4 | -import javax.websocket.CloseReason; | |
5 | -import javax.websocket.OnClose; | |
6 | -import javax.websocket.OnMessage; | |
7 | -import javax.websocket.OnOpen; | |
8 | -import javax.websocket.Session; | |
3 | +import org.slf4j.Logger; | |
4 | +import org.slf4j.LoggerFactory; | |
5 | + | |
6 | +import javax.annotation.PostConstruct; | |
7 | +import javax.websocket.*; | |
8 | +import javax.websocket.server.PathParam; | |
9 | 9 | import javax.websocket.server.ServerEndpoint; |
10 | 10 | import java.io.IOException; |
11 | +import java.util.Collections; | |
12 | +import java.util.HashSet; | |
13 | +import java.util.Set; | |
11 | 14 | |
12 | 15 | /** |
13 | 16 | * Stub für die WebSocket-Unterstützung. |
17 | + * | |
18 | + * Muss bei Bedarf noch um ein JSON-Marshalling erweitert werden. | |
19 | + * | |
20 | + * Aufgepasst: Mittels CDI kann nur @ApplicationScoped injiziert werden, | |
21 | + * da während eines WebSocket-Callbacks kein Session-Kontext aktiv ist. | |
14 | 22 | */ |
15 | -@ApplicationScoped | |
16 | 23 | @ServerEndpoint(WebSocketController.serverEndpointPath) |
17 | 24 | public class WebSocketController { |
18 | 25 | |
19 | - static final String serverEndpointPath = "/ws/api/v1/anEndpoint"; | |
26 | + static final String serverEndpointPath = "/ws/api/v1/anEndpoint/{aParameter}"; | |
27 | + | |
28 | + private static Set<Session> peers = Collections.synchronizedSet(new HashSet<>()); | |
29 | + | |
30 | + private Logger logger; | |
31 | + | |
32 | + @PostConstruct | |
33 | + private void init() { | |
34 | + logger = LoggerFactory.getLogger(this.getClass().getSimpleName()); | |
35 | + } | |
20 | 36 | |
21 | 37 | /** |
22 | 38 | * Callback-Methode für das Öffnen einer neuen WebSocket-Verbindung. |
... | ... | @@ -25,7 +41,8 @@ public class WebSocketController { |
25 | 41 | */ |
26 | 42 | @OnOpen |
27 | 43 | public void onOpen(Session session) { |
28 | - System.out.println("WebSocket opened with session id #" + session.getId()); | |
44 | + logger.info("WebSocket opened with session id #{}", session.getId()); | |
45 | + peers.add(session); | |
29 | 46 | } |
30 | 47 | |
31 | 48 | /** |
... | ... | @@ -35,16 +52,27 @@ public class WebSocketController { |
35 | 52 | * @param session das {@link Session}-Objekt der sendenden WebSocket-Verbindung |
36 | 53 | */ |
37 | 54 | @OnMessage |
38 | - public void onMessage(String message, Session session) { | |
39 | - System.out.println("WebSocket Message '" + message + "' received by session id #" + session.getId()); | |
55 | + public void onMessage(String message, Session session, @PathParam("aParameter") String param) { | |
56 | + logger.info("WebSocket Message '{}/{}' received by session id #{}", message, param, session.getId()); | |
40 | 57 | try { |
41 | - session.getBasicRemote().sendText(message); | |
58 | + // wir senden die empfangene Nachricht gleich wieder zurück | |
59 | + session.getBasicRemote().sendText(String.format("%s/%s", message, param)); | |
42 | 60 | } catch (IOException e) { |
43 | 61 | e.printStackTrace(); |
44 | 62 | } |
45 | 63 | } |
46 | 64 | |
47 | 65 | /** |
66 | + * Callback-Methode, wenn in der WebSocket ein Problem auftrat. | |
67 | + * | |
68 | + * @param t die Exception | |
69 | + */ | |
70 | + @OnError | |
71 | + public void onError(Throwable t) { | |
72 | + logger.error("WebSocket Error '{}' occured!", t.getMessage()); | |
73 | + } | |
74 | + | |
75 | + /** | |
48 | 76 | * Callback-Methode für das Schließen einer geöffneten WebSocket-Verbindung. |
49 | 77 | * |
50 | 78 | * @param reason die Ursache für das Schließen der WebSocket-Verbindung |
... | ... | @@ -52,7 +80,8 @@ public class WebSocketController { |
52 | 80 | */ |
53 | 81 | @OnClose |
54 | 82 | public void onClose(CloseReason reason, Session session) { |
55 | - System.out.println("Closing WebSocket due to " + reason.getReasonPhrase() + " by id #" + session.getId()); | |
83 | + logger.info("Closing WebSocket due to '{}' by session id #{}", reason.getReasonPhrase(), session.getId()); | |
84 | + peers.remove(session); | |
56 | 85 | } |
57 | 86 | |
58 | 87 | } | ... | ... |
src/main/webapp/websockets.html
... | ... | @@ -6,7 +6,7 @@ |
6 | 6 | </head> |
7 | 7 | <body> |
8 | 8 | <script> |
9 | - var url = "ws://localhost:8080/swxercise/ws/api/v1/anEndpoint"; | |
9 | + var url = "ws://localhost:8080/swxercise/ws/api/v1/anEndpoint/4711"; | |
10 | 10 | var webSocket = new WebSocket(url); |
11 | 11 | |
12 | 12 | // Callback-Methoden für die WebSocket-Kommunikation | ... | ... |