commit
efc1277b0a
|
@ -18,6 +18,15 @@
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.projectreactor</groupId>
|
||||||
|
<artifactId>reactor-core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.javafaker</groupId>
|
||||||
|
<artifactId>javafaker</artifactId>
|
||||||
|
<version>1.0.2</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
|
|
|
@ -3,8 +3,10 @@ package com.baeldung;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableScheduling
|
||||||
public class SpringBootApp extends SpringBootServletInitializer {
|
public class SpringBootApp extends SpringBootServletInitializer {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(SpringBootApp.class, args);
|
SpringApplication.run(SpringBootApp.class, args);
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.websockets;
|
||||||
|
|
||||||
|
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||||
|
import org.springframework.messaging.handler.annotation.SendTo;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class BotsController {
|
||||||
|
|
||||||
|
@MessageMapping("/chatwithbots")
|
||||||
|
@SendTo("/topic/pushmessages")
|
||||||
|
public OutputMessage send(final Message message) throws Exception {
|
||||||
|
|
||||||
|
final String time = new SimpleDateFormat("HH:mm").format(new Date());
|
||||||
|
return new OutputMessage(message.getFrom(), message.getText(), time);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.websockets;
|
||||||
|
|
||||||
|
import com.github.javafaker.Faker;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ReactiveScheduledPushMessages implements InitializingBean {
|
||||||
|
|
||||||
|
private final SimpMessagingTemplate simpMessagingTemplate;
|
||||||
|
|
||||||
|
private final Faker faker;
|
||||||
|
|
||||||
|
public ReactiveScheduledPushMessages(SimpMessagingTemplate simpMessagingTemplate) {
|
||||||
|
this.simpMessagingTemplate = simpMessagingTemplate;
|
||||||
|
this.faker = new Faker();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() throws Exception {
|
||||||
|
Flux.interval(Duration.ofSeconds(4L))
|
||||||
|
.map((n) -> new OutputMessage(faker.backToTheFuture().character(), faker.backToTheFuture().quote(),
|
||||||
|
new SimpleDateFormat("HH:mm").format(new Date())))
|
||||||
|
.subscribe(message -> simpMessagingTemplate.convertAndSend("/topic/pushmessages", message));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.websockets;
|
||||||
|
|
||||||
|
|
||||||
|
import com.github.javafaker.Faker;
|
||||||
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ScheduledPushMessages {
|
||||||
|
|
||||||
|
private final SimpMessagingTemplate simpMessagingTemplate;
|
||||||
|
|
||||||
|
private final Faker faker;
|
||||||
|
|
||||||
|
public ScheduledPushMessages(SimpMessagingTemplate simpMessagingTemplate) {
|
||||||
|
this.simpMessagingTemplate = simpMessagingTemplate;
|
||||||
|
faker = new Faker();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(fixedRate = 5000)
|
||||||
|
public void sendMessage() {
|
||||||
|
final String time = new SimpleDateFormat("HH:mm").format(new Date());
|
||||||
|
simpMessagingTemplate.convertAndSend("/topic/pushmessages",
|
||||||
|
new OutputMessage("Chuck Norris", faker.chuckNorris().fact(), time));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -20,6 +20,8 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
||||||
public void registerStompEndpoints(final StompEndpointRegistry registry) {
|
public void registerStompEndpoints(final StompEndpointRegistry registry) {
|
||||||
registry.addEndpoint("/chat");
|
registry.addEndpoint("/chat");
|
||||||
registry.addEndpoint("/chat").withSockJS();
|
registry.addEndpoint("/chat").withSockJS();
|
||||||
|
registry.addEndpoint("/chatwithbots");
|
||||||
|
registry.addEndpoint("/chatwithbots").withSockJS();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,88 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Chat WebSocket</title>
|
||||||
|
|
||||||
|
<script src="resources/js/sockjs-0.3.4.js"></script>
|
||||||
|
<script src="resources/js/stomp.js"></script>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
|
||||||
|
var stompClient = null;
|
||||||
|
|
||||||
|
function setConnected(connected) {
|
||||||
|
|
||||||
|
document.getElementById('connect').disabled = connected;
|
||||||
|
document.getElementById('disconnect').disabled = !connected;
|
||||||
|
document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
|
||||||
|
document.getElementById('response').innerHTML = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function connect() {
|
||||||
|
|
||||||
|
var socket = new SockJS('/chatwithbots');
|
||||||
|
stompClient = Stomp.over(socket);
|
||||||
|
|
||||||
|
stompClient.connect({}, function(frame) {
|
||||||
|
|
||||||
|
setConnected(true);
|
||||||
|
console.log('Connected: ' + frame);
|
||||||
|
stompClient.subscribe('/topic/pushmessages', function(messageOutput) {
|
||||||
|
|
||||||
|
showMessageOutput(JSON.parse(messageOutput.body));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function disconnect() {
|
||||||
|
|
||||||
|
if(stompClient != null) {
|
||||||
|
stompClient.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
setConnected(false);
|
||||||
|
console.log("Disconnected");
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendMessage() {
|
||||||
|
|
||||||
|
var from = document.getElementById('from').value;
|
||||||
|
var text = document.getElementById('text').value;
|
||||||
|
stompClient.send("/app/chatwithbots", {}, JSON.stringify({'from':from, 'text':text}));
|
||||||
|
}
|
||||||
|
|
||||||
|
function showMessageOutput(messageOutput) {
|
||||||
|
|
||||||
|
var response = document.getElementById('response');
|
||||||
|
var p = document.createElement('p');
|
||||||
|
p.style.wordWrap = 'break-word';
|
||||||
|
p.appendChild(document.createTextNode(messageOutput.from + ": " + messageOutput.text + " (" + messageOutput.time + ")"));
|
||||||
|
response.appendChild(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body onload="disconnect()">
|
||||||
|
|
||||||
|
<div>
|
||||||
|
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<input type="text" id="from" placeholder="Choose a nickname"/>
|
||||||
|
</div>
|
||||||
|
<br />
|
||||||
|
<div>
|
||||||
|
<button id="connect" onclick="connect();">Connect</button>
|
||||||
|
<button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button>
|
||||||
|
</div>
|
||||||
|
<br />
|
||||||
|
<div id="conversationDiv">
|
||||||
|
<input type="text" id="text" placeholder="Write a message..."/>
|
||||||
|
<button id="sendMessage" onclick="sendMessage();">Send</button>
|
||||||
|
<p id="response"></p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue