Bläddra i källkod

did somethings sorta

David Thornley 6 år sedan
förälder
incheckning
795747400f

+ 4
- 1
pom.xml Visa fil

@@ -39,7 +39,10 @@
39 39
             <!--<artifactId>mysql-connector-java</artifactId>-->
40 40
             <!--<scope>runtime</scope>-->
41 41
         <!--</dependency>-->
42
-
42
+        <dependency>
43
+            <groupId>org.springframework.boot</groupId>
44
+            <artifactId>spring-boot-starter-websocket</artifactId>
45
+        </dependency>
43 46
         <dependency>
44 47
             <groupId>org.springframework.boot</groupId>
45 48
             <artifactId>spring-boot-starter-data-rest</artifactId>

+ 27
- 0
src/main/java/com/ziplinegreen/vault/Config/WebSocketConfig.java Visa fil

@@ -0,0 +1,27 @@
1
+package com.ziplinegreen.vault.Config;
2
+
3
+
4
+import org.springframework.context.annotation.ComponentScan;
5
+import org.springframework.context.annotation.Configuration;
6
+import org.springframework.messaging.simp.config.MessageBrokerRegistry;
7
+import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
8
+import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
9
+import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
10
+
11
+@Configuration
12
+@EnableWebSocketMessageBroker
13
+@ComponentScan(basePackages = "com.ziplinegreen.vault")
14
+public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
15
+
16
+    @Override
17
+    public void configureMessageBroker(MessageBrokerRegistry config) {
18
+        config.enableSimpleBroker("/topic"); //marks
19
+        config.setApplicationDestinationPrefixes("/app"); //where the frontend sends things
20
+    }
21
+
22
+    @Override
23
+    public void registerStompEndpoints(StompEndpointRegistry registry) {
24
+        registry.addEndpoint("/posts");
25
+        registry.addEndpoint("/posts").withSockJS();
26
+    }
27
+}

+ 101
- 0
src/main/resources/static/app.js Visa fil

@@ -0,0 +1,101 @@
1
+// var stompClient = null;
2
+//
3
+// function setConnected(connected) {
4
+//     $("#connect").prop("disabled", connected);
5
+//     $("#disconnect").prop("disabled", !connected);
6
+//     if (connected) {
7
+//         $("#conversation").show();
8
+//     }
9
+//     else {
10
+//         $("#conversation").hide();
11
+//     }
12
+//     $("#greetings").html("");
13
+// }
14
+//
15
+// function connect() {
16
+//     var socket = new SockJS('/gs-guide-websocket');
17
+//     stompClient = Stomp.over(socket);
18
+//     stompClient.connect({}, function (frame) {
19
+//         setConnected(true);
20
+//         console.log('Connected: ' + frame);
21
+//         stompClient.subscribe('/topic/posts', function (post) {
22
+//             showGreeting(JSON.parse(post.body).message);
23
+//         });
24
+//     });
25
+// }
26
+//
27
+// function disconnect() {
28
+//     if (stompClient !== null) {
29
+//         stompClient.disconnect();
30
+//     }
31
+//     setConnected(false);
32
+//     console.log("Disconnected");
33
+// }
34
+//
35
+// function sendName() {
36
+//     stompClient.send("/users/1/posts/", {}, JSON.stringify({'message': $("#message").val(), 'userId': '1'}));
37
+// }
38
+//
39
+// function showGreeting(message) {
40
+//     $("#greetings").append("<tr><td>" + message + "</td></tr>");
41
+// }
42
+//
43
+// $(function () {
44
+//     $("form").on('submit', function (e) {
45
+//         e.preventDefault();
46
+//     });
47
+//     $( "#connect" ).click(function() { connect(); });
48
+//     $( "#disconnect" ).click(function() { disconnect(); });
49
+//     $( "#send" ).click(function() { sendName(); });
50
+// });
51
+
52
+var stompClient = null;
53
+
54
+function setConnected(connected) {
55
+    $('#connect').prop("disabled", connected);
56
+    $('#disconnect').prop("disabled", !connected);
57
+    if (connected) {
58
+        $("#conversation").show();
59
+    }
60
+    else {
61
+        $("#conversation").hide();
62
+    }
63
+    $("#greetings").html("");
64
+}
65
+
66
+function connect() {
67
+    var socket = new SockJS('/gs-guide-websocket');
68
+    stompClient = Stomp.over(socket);
69
+    stompClient.connect({}, function (frame) {
70
+        setConnected(true);
71
+        console.log('Connected: ' + frame);
72
+        stompClient.subscribe('/topic/greetings', function (greeting) {
73
+            showGreeting(JSON.parse(greeting.body).content);
74
+        });
75
+    });
76
+}
77
+
78
+function disconnect() {
79
+    if (stompClient !== null) {
80
+        stompClient.disconnect();
81
+    }
82
+    setConnected(false);
83
+    console.log("Disconnected");
84
+}
85
+
86
+function sendName() {
87
+    stompClient.send("/app/hello", {}, JSON.stringify({'name': $("#name").val()}));
88
+}
89
+
90
+function showGreeting(message) {
91
+    $("#greetings").append("<tr><td>" + message + "</td></tr>");
92
+}
93
+
94
+$(function () {
95
+    $("form").on('submit', function (e) {
96
+        e.preventDefault();
97
+    });
98
+    $( "#connect" ).click(function() { connect(); });
99
+    $( "#disconnect" ).click(function() { disconnect(); });
100
+    $( "#send" ).click(function() { sendName(); });
101
+});

+ 53
- 0
src/main/resources/static/index.html Visa fil

@@ -0,0 +1,53 @@
1
+<!DOCTYPE html>
2
+<html>
3
+<head>
4
+    <title>Hello WebSocket</title>
5
+    <link href="/webjars/bootstrap/css/bootstrap.min.css" rel="stylesheet">
6
+    <link href="/main.css" rel="stylesheet">
7
+    <script src="/webjars/jquery/jquery.min.js"></script>
8
+    <script src="/webjars/sockjs-client/sockjs.min.js"></script>
9
+    <script src="/webjars/stomp-websocket/stomp.min.js"></script>
10
+    <script src="/app.js"></script>
11
+</head>
12
+<body>
13
+<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websocket relies on Javascript being
14
+    enabled. Please enable
15
+    Javascript and reload this page!</h2></noscript>
16
+<div id="main-content" class="container">
17
+    <div class="row">
18
+        <div class="col-md-6">
19
+            <form class="form-inline">
20
+                <div class="form-group">
21
+                    <label for="connect">WebSocket connection:</label>
22
+                    <button id="connect" class="btn btn-default" type="submit">Connect</button>
23
+                    <button id="disconnect" class="btn btn-default" type="submit" disabled="disabled">Disconnect
24
+                    </button>
25
+                </div>
26
+            </form>
27
+        </div>
28
+        <div class="col-md-6">
29
+            <form class="form-inline">
30
+                <div class="form-group">
31
+                    <label for="message">Message?</label>
32
+                    <input type="text" id="message" class="form-control" placeholder="What is your message...">
33
+                </div>
34
+                <button id="send" class="btn btn-default" type="submit">Send</button>
35
+            </form>
36
+        </div>
37
+    </div>
38
+    <div class="row">
39
+        <div class="col-md-12">
40
+            <table id="conversation" class="table table-striped">
41
+                <thead>
42
+                <tr>
43
+                    <th>Messages</th>
44
+                </tr>
45
+                </thead>
46
+                <tbody id="messages">
47
+                </tbody>
48
+            </table>
49
+        </div>
50
+    </div>
51
+</div>
52
+</body>
53
+</html>