Add websocket example
This commit is contained in:
parent
4d1dd06a26
commit
889b266107
|
@ -8,19 +8,12 @@ import org.baeldung.model.OutputMessage;
|
||||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||||
import org.springframework.messaging.handler.annotation.SendTo;
|
import org.springframework.messaging.handler.annotation.SendTo;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class ChatController {
|
public class ChatController {
|
||||||
|
|
||||||
@RequestMapping(value = "/showChat", method = RequestMethod.GET)
|
|
||||||
public String displayChat() {
|
|
||||||
return "chat";
|
|
||||||
}
|
|
||||||
|
|
||||||
@MessageMapping("/chat")
|
@MessageMapping("/chat")
|
||||||
@SendTo("/topic/")
|
@SendTo("/topic/messages")
|
||||||
public OutputMessage send(final Message message) throws Exception {
|
public OutputMessage send(final Message message) throws Exception {
|
||||||
|
|
||||||
final String time = new SimpleDateFormat("HH:mm").format(new Date());
|
final String time = new SimpleDateFormat("HH:mm").format(new Date());
|
||||||
|
|
|
@ -1,13 +1,9 @@
|
||||||
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Chat WebSocket</title>
|
<title>Chat WebSocket</title>
|
||||||
|
|
||||||
<spring:url value="/resources/js/sockjs-0.3.4.js" var="sockjs" />
|
<script src="./js/sockjs-0.3.4.js"></script>
|
||||||
<spring:url value="/resources/js/stomp.js" var="stomp" />
|
<script src="./js/stomp.js"></script>
|
||||||
|
|
||||||
<script src="${sockjs}"></script>
|
|
||||||
<script src="${stomp}"></script>
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
|
||||||
|
@ -30,7 +26,8 @@
|
||||||
|
|
||||||
setConnected(true);
|
setConnected(true);
|
||||||
console.log('Connected: ' + frame);
|
console.log('Connected: ' + frame);
|
||||||
stompClient.subscribe('/topic/message', function(messageOutput) {
|
stompClient.subscribe('/topic/messages', function(messageOutput) {
|
||||||
|
|
||||||
showMessageOutput(JSON.parse(messageOutput.body));
|
showMessageOutput(JSON.parse(messageOutput.body));
|
||||||
});
|
});
|
||||||
});
|
});
|
Loading…
Reference in New Issue