Skip to content

Commit 8361797

Browse files
committed
Add WebClient for local debugging
1 parent 6cf3608 commit 8361797

File tree

6 files changed

+151
-2
lines changed

6 files changed

+151
-2
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ dependencies {
3232
compile("org.springframework.boot:spring-boot-starter-web")
3333
compile("org.springframework.boot:spring-boot-starter-actuator")
3434
//compile("org.springframework.boot:spring-boot-starter-remote-shell")
35+
compile("org.springframework.boot:spring-boot-starter-websocket")
36+
compile("org.springframework:spring-messaging")
3537
compile("org.springframework.boot:spring-boot-starter-test")
3638
compile("org.projectlombok:lombok:1.14.8")
3739
compile("com.google.code.findbugs:jsr305:3.0.0")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package de.votesapp;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
5+
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
6+
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
7+
8+
@Configuration
9+
@EnableWebSocketMessageBroker
10+
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
11+
12+
@Override
13+
public void registerStompEndpoints(final StompEndpointRegistry registry) {
14+
registry.addEndpoint("/whatsapp").withSockJS();
15+
}
16+
}

src/main/java/de/votesapp/client/ConsoleClient.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
import lombok.SneakyThrows;
1212

1313
import org.springframework.context.annotation.Profile;
14-
import org.springframework.stereotype.Component;
1514

1615
/**
1716
* This makes it run on a developers pc without a connection to yowsup. Later we
1817
* could replace this by an more elegant ui.
1918
*/
20-
@Component
19+
// @Component
2120
@Profile("!yowsup")
2221
public class ConsoleClient implements WhatsAppClient {
2322

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package de.votesapp.client;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.UUID;
6+
7+
import lombok.Data;
8+
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.context.annotation.Profile;
11+
import org.springframework.messaging.simp.SimpMessagingTemplate;
12+
import org.springframework.web.bind.annotation.RequestBody;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
/**
17+
* This makes it run on a developers pc without a connection to yowsup. Later we
18+
* could replace this by an more elegant ui.
19+
*/
20+
@RestController
21+
@Profile("!yowsup")
22+
public class WebClient implements WhatsAppClient {
23+
24+
@Autowired
25+
private SimpMessagingTemplate brokerMessagingTemplate;
26+
27+
private volatile List<GroupMessage> groupMessages = new ArrayList<>();
28+
29+
@Override
30+
public void sendGroupMessage(final GroupMessage messageToSend) {
31+
brokerMessagingTemplate.convertAndSend("/receive", messageToSend);
32+
}
33+
34+
@Override
35+
public GroupMessage[] fetchGroupMessages() {
36+
synchronized (groupMessages) {
37+
final GroupMessage[] messages = groupMessages.toArray(new GroupMessage[0]);
38+
groupMessages.clear();
39+
return messages;
40+
}
41+
}
42+
43+
@RequestMapping("/send")
44+
public void sendPerRest(@RequestBody final SendModel sm) {
45+
synchronized (groupMessages) {
46+
final GroupMessage generatedMessage = new GroupMessage(UUID.randomUUID().toString(), sm.getGroup(), sm.getPhone(), null, sm.getText());
47+
groupMessages.add(generatedMessage);
48+
}
49+
}
50+
51+
@Data
52+
public static class SendModel {
53+
private String phone;
54+
private String group;
55+
private String text;
56+
}
57+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>VotesApp WebClient Frontend</title>
5+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
6+
<script src="//cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js"></script>
7+
<script src="stomp.min.js"></script>
8+
<script type="text/javascript">
9+
$(function() {
10+
function connect() {
11+
var socket = new SockJS('/whatsapp');
12+
stompClient = Stomp.over(socket);
13+
stompClient.connect({}, function(frame) {
14+
console.log('Connected: ' + frame);
15+
stompClient.subscribe('/receive', function(msg){
16+
var body = JSON.parse(msg.body);
17+
var content = { text: body.text, groupId: body.groupId };
18+
$("#msgs").append('<div class="alert alert-info" role="alert"><strong>' + body.groupId + '</strong> ' + body.text +'</div>');
19+
});
20+
});
21+
}
22+
23+
connect();
24+
25+
$('#sendButton').on('click', function() {
26+
var phone = $('#phone').val();
27+
var group = $('#group').val();
28+
var text = $('#text').val();
29+
$.ajax({
30+
type: "POST",
31+
url: "/send",
32+
data: JSON.stringify({phone: phone, group: group, text: text}),
33+
contentType: "application/json; charset=utf-8",
34+
dataType: "json"
35+
});
36+
});
37+
});
38+
</script>
39+
40+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
41+
</head>
42+
<body>
43+
<noscript><h2 style="color: #ff0000">Please enable Javascript and reload this page!</h2></noscript>
44+
<div class="container">
45+
<h1>Debug VotestApp Interface</h1>
46+
<form>
47+
<div class="form-group">
48+
<label for="phone">Phone</label>
49+
<input type="text" class="form-control" id="phone" placeholder="Enter phone number">
50+
</div>
51+
<div class="form-group">
52+
<label for="group">Group</label>
53+
<input type="text" class="form-control" id="group" placeholder="Enter group">
54+
</div>
55+
<div class="form-group">
56+
<label for="text">Text</label>
57+
<input type="text" class="form-control" id="text" placeholder="Enter message text">
58+
</div>
59+
<button type="button" class="btn btn-lg btn-primary" id="sendButton">Send Message</button>
60+
</form>
61+
<p>
62+
<div id="msgs"></div>
63+
</div>
64+
65+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
66+
</body>
67+
</html>

src/main/resources/static/stomp.min.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)