|
@@ -0,0 +1,76 @@
|
|
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
|
+ $("#messages").html("");
|
|
13
|
+}
|
|
14
|
+
|
|
15
|
+function connect() {
|
|
16
|
+ var socket = new SockJS('/vault-socket');
|
|
17
|
+ stompClient = Stomp.over(socket);
|
|
18
|
+ stompClient.connect({}, function (frame) {
|
|
19
|
+ setConnected(true);
|
|
20
|
+ console.log('Connected: ' + frame);
|
|
21
|
+ stompClient.subscribe('/topic/posts', function (greeting) {
|
|
22
|
+ showGreeting(JSON.parse(greeting.body).message, JSON.parse(greeting.body).userName);
|
|
23
|
+ //showGreeting()
|
|
24
|
+ });
|
|
25
|
+ });
|
|
26
|
+}
|
|
27
|
+
|
|
28
|
+function disconnect() {
|
|
29
|
+ if (stompClient !== null) {
|
|
30
|
+ stompClient.disconnect();
|
|
31
|
+ }
|
|
32
|
+ setConnected(false);
|
|
33
|
+ console.log("Disconnected");
|
|
34
|
+}
|
|
35
|
+
|
|
36
|
+var userId = 1;
|
|
37
|
+var userPostUrl = "/app/posts/1";
|
|
38
|
+
|
|
39
|
+function user1() {
|
|
40
|
+ $('#user1').prop("disabled", true);
|
|
41
|
+ $('#user2').prop("disabled", false);
|
|
42
|
+ userId = 1;
|
|
43
|
+}
|
|
44
|
+
|
|
45
|
+function user2() {
|
|
46
|
+ $('#user1').prop("disabled", false);
|
|
47
|
+ $('#user2').prop("disabled", true);
|
|
48
|
+ userId = 2;
|
|
49
|
+ userPostUrl = "/app/posts/2";
|
|
50
|
+}
|
|
51
|
+
|
|
52
|
+// function sendName() {
|
|
53
|
+// stompClient.send("/app/posts/1", {}, JSON.stringify({'id': '','message': $("#message").val(),'userId': '1'}));
|
|
54
|
+// }
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+function sendName() {
|
|
59
|
+
|
|
60
|
+ stompClient.send(userPostUrl, {}, JSON.stringify({'id': '','message': $("#message").val(),'userId': userId}))
|
|
61
|
+}
|
|
62
|
+
|
|
63
|
+function showGreeting(message, userName) {
|
|
64
|
+ $("#messages").append("<tr><td>" + userName + ": " + message + "</td></tr>");
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+$(function () {
|
|
68
|
+ $("form").on('submit', function (e) {
|
|
69
|
+ e.preventDefault();
|
|
70
|
+ });
|
|
71
|
+ $( "#connect" ).click(function() { connect(); });
|
|
72
|
+ $( "#disconnect" ).click(function() { disconnect(); });
|
|
73
|
+ $( "#send" ).click(function() { sendName(); });
|
|
74
|
+ $( "#user1" ).click(function() { user1(); });
|
|
75
|
+ $( "#user2" ).click(function() { user2(); })
|
|
76
|
+});
|