Browse Source

Add basic websocket handler.

pull/437/head
Chris Smith 8 years ago
parent
commit
8aaef9f2b8

+ 10
- 0
ui_web2/res/www/index.html View File

@@ -0,0 +1,10 @@
1
+<!DOCTYPE html>
2
+<html>
3
+  <head>
4
+    <title>DMDirc</title>
5
+      <script src="js/socket.js"></script>
6
+  </head>
7
+  <body>
8
+    HELLO WORLD
9
+  </body>
10
+</html>

+ 20
- 0
ui_web2/res/www/js/socket.js View File

@@ -0,0 +1,20 @@
1
+socket = null;
2
+
3
+function getAddress() {
4
+    var loc = window.location;
5
+    var uri = loc.protocol === "https:" ? "wss:" : "ws:";
6
+    uri += "//" + loc.host + "/ws";
7
+    return uri;
8
+}
9
+
10
+function connect() {
11
+    socket = new WebSocket(getAddress());
12
+
13
+    socket.onmessage = function(event) {
14
+      console.log(event.data);
15
+    }
16
+}
17
+
18
+function send(data) {
19
+    socket.send(JSON.stringify(data));
20
+}

+ 2
- 0
ui_web2/src/com/dmdirc/addons/ui_web2/WebServer.java View File

@@ -37,6 +37,8 @@ public class WebServer {
37 37
 
38 38
     public void start() {
39 39
         Spark.port(port);
40
+        Spark.webSocket("/ws", WebSocketHandler.class);
41
+        Spark.staticFileLocation("/www");
40 42
         Spark.get("/test", (request, response) -> "HELLO");
41 43
     }
42 44
 

+ 59
- 0
ui_web2/src/com/dmdirc/addons/ui_web2/WebSocketHandler.java View File

@@ -0,0 +1,59 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.ui_web2;
24
+
25
+import java.io.IOException;
26
+import java.util.Queue;
27
+import java.util.concurrent.ConcurrentLinkedQueue;
28
+
29
+import org.eclipse.jetty.websocket.api.Session;
30
+import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
31
+import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
32
+import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
33
+import org.eclipse.jetty.websocket.api.annotations.WebSocket;
34
+
35
+/**
36
+ * Handles websocket connections.
37
+ */
38
+@WebSocket
39
+public class WebSocketHandler {
40
+
41
+    private static final Queue<Session> sessions = new ConcurrentLinkedQueue<>();
42
+
43
+    @OnWebSocketConnect
44
+    public void connected(final Session session) {
45
+        sessions.add(session);
46
+    }
47
+
48
+    @OnWebSocketClose
49
+    public void closed(final Session session, final int statusCode, final String reason) {
50
+        sessions.remove(session);
51
+    }
52
+
53
+    @OnWebSocketMessage
54
+    public void message(final Session session, final String message) throws IOException {
55
+        // Echo the message back, for testing.
56
+        session.getRemote().sendString(message);
57
+    }
58
+
59
+}

Loading…
Cancel
Save