Commit 6ea89b5fb2ee700d14c64167fb8ec574e3da02f5

Authored by Thomas Ziemer
1 parent 868b6113

feature: Klassen-Stub für den "WebSocketController" hinzugefügt

... ... @@ -24,6 +24,7 @@
24 24 <slf4j-version>1.7.25</slf4j-version>
25 25 <jglue-version>3.1.4</jglue-version>
26 26 <jackson-version>2.8.8.1</jackson-version>
  27 + <jetty-version>9.4.8.v20171121</jetty-version>
27 28  
28 29 <!-- Substituiert die Platzhalter in der "persistence.xml" -->
29 30 <TEST_SCHEMA_NAME>swxercise_test</TEST_SCHEMA_NAME>
... ... @@ -97,6 +98,24 @@
97 98 <version>${jglue-version}</version>
98 99 <scope>test</scope>
99 100 </dependency>
  101 + <dependency>
  102 + <groupId>org.eclipse.jetty</groupId>
  103 + <artifactId>jetty-server</artifactId>
  104 + <version>${jetty-version}</version>
  105 + <scope>provided</scope>
  106 + </dependency>
  107 + <dependency>
  108 + <groupId>org.eclipse.jetty</groupId>
  109 + <artifactId>jetty-servlet</artifactId>
  110 + <version>${jetty-version}</version>
  111 + <scope>provided</scope>
  112 + </dependency>
  113 + <dependency>
  114 + <groupId>org.eclipse.jetty.websocket</groupId>
  115 + <artifactId>javax-websocket-server-impl</artifactId>
  116 + <version>${jetty-version}</version>
  117 + <scope>provided</scope>
  118 + </dependency>
100 119 <!-- Sorgt für eine schnellere Verwaltung der JSON-Umwandlung im REST-Kontext -->
101 120 <!-- dependency>
102 121 <groupId>com.fasterxml.jackson.core</groupId>
... ... @@ -124,6 +143,11 @@
124 143 <target>${java-version}</target>
125 144 </configuration>
126 145 </plugin>
  146 + <plugin>
  147 + <groupId>org.eclipse.jetty</groupId>
  148 + <artifactId>jetty-maven-plugin</artifactId>
  149 + <version>${jetty-version}</version>
  150 + </plugin>
127 151 </plugins>
128 152 </build>
129 153 </project>
... ...
src/main/java/net/ziemers/swxercise/ui/ws/WebSocketController.java 0 → 100644
  1 +package net.ziemers.swxercise.ui.ws;
  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;
  9 +import javax.websocket.server.ServerEndpoint;
  10 +import java.io.IOException;
  11 +
  12 +/**
  13 + * Stub für die WebSocket-Unterstützung.
  14 + */
  15 +@ApplicationScoped
  16 +@ServerEndpoint(WebSocketController.serverEndpointPath)
  17 +public class WebSocketController {
  18 +
  19 + static final String serverEndpointPath = "/ws/api/v1/anEndpoint";
  20 +
  21 + /**
  22 + * Callback-Methode für das Öffnen einer neuen WebSocket-Verbindung.
  23 + *
  24 + * @param session das {@link Session}-Objekt der neuen WebSocket-Verbindung
  25 + */
  26 + @OnOpen
  27 + public void onOpen(Session session) {
  28 + System.out.println("WebSocket opened with session id #" + session.getId());
  29 + }
  30 +
  31 + /**
  32 + * Callback-Methode, die den Empfang einer neuen WebSocket-Nachricht signalisiert.
  33 + *
  34 + * @param message der Inhalt der WebSocket-Nachricht
  35 + * @param session das {@link Session}-Objekt der sendenden WebSocket-Verbindung
  36 + */
  37 + @OnMessage
  38 + public void onMessage(String message, Session session) {
  39 + System.out.println("WebSocket Message '" + message + "' received by session id #" + session.getId());
  40 + try {
  41 + session.getBasicRemote().sendText(message);
  42 + } catch (IOException e) {
  43 + e.printStackTrace();
  44 + }
  45 + }
  46 +
  47 + /**
  48 + * Callback-Methode für das Schließen einer geöffneten WebSocket-Verbindung.
  49 + *
  50 + * @param reason die Ursache für das Schließen der WebSocket-Verbindung
  51 + * @param session das {@link Session}-Objekt der geschlossenen WebSocket-Verbindung
  52 + */
  53 + @OnClose
  54 + public void onClose(CloseReason reason, Session session) {
  55 + System.out.println("Closing WebSocket due to " + reason.getReasonPhrase() + " by id #" + session.getId());
  56 + }
  57 +
  58 +}
... ...
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/test";
  9 + var url = "ws://localhost:8080/swxercise/ws/api/v1/anEndpoint";
10 10 var webSocket = new WebSocket(url);
11 11  
12 12 // Callback-Methoden für die WebSocket-Kommunikation
... ... @@ -32,10 +32,8 @@
32 32 }
33 33  
34 34 function sendWebSocketMessage() {
35   - if (webSocket.send('Hello World!'))
36   - console.log("WebSocket Message sent.");
37   - else
38   - alert("WebSocket Message not sent!");
  35 + webSocket.send('Hello World!');
  36 + console.log("Tried to send WebSocket Message.");
39 37 }
40 38 </script>
41 39 <button onclick="sendWebSocketMessage()">Klick mich.</button>
... ...