blog: chatting - connection test

This commit is contained in:
haerong22
2022-09-03 17:46:16 +09:00
parent 4011a6a520
commit 40f909fb7a
4 changed files with 68 additions and 4 deletions

View File

@@ -0,0 +1,62 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js" integrity="sha512-iKDtgDyTHjAitUDdLljGhenhPwrbBfqTKWO1mkhSFH3A7blITC9MhYon6SjnMhp4o0rADGw9yAC6EW4t5a4K3g==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.6.1/sockjs.min.js"></script>
<body>
<button onclick="connect()">Connect</button>
<hr/>
<input id="sub" type="text" />
<button onclick="subscribe()">Subscribe</button>
<hr/>
<input id="pub" type="text" />
<button onclick="publish()">Publish</button>
<script>
let stompClient;
const connect = () => {
const socket = new SockJS('http://localhost:28080/ws-stomp')
stompClient = Stomp.over(socket);
stompClient.connect({} , (frame) => {
console.log(frame);
});
}
const subscribe = () => {
const topic = document.getElementById("sub").value;
console.log(topic)
stompClient.subscribe("/sub/" + topic, (response) => {
console.log(response);
})
}
const publish = () => {
const topic = document.getElementById("sub").value;
const message = document.getElementById("pub").value;
const request = {
"message": message,
"id": 1,
}
stompClient.send("/pub/" + topic, {}, JSON.stringify(request));
}
</script>
</body>
</html>

View File

@@ -15,4 +15,5 @@ public class ChatMessage {
private String message;
private LocalDateTime date;
private TestEnum testEnum;
private String id;
}

View File

@@ -16,8 +16,8 @@ import java.io.IOException;
public class CorsFilter implements Filter {
private final String[] allowOriginDomains = {
"http://localhost:5500",
"http://127.0.0.1:5500"
"http://localhost:63342",
"http://127.0.0.1:63342"
};
@Override

View File

@@ -20,8 +20,9 @@ public class RedisSubscriber {
public void sendMessage(String message) {
try {
ChatMessage chatMessage = objectMapper.readValue(message, ChatMessage.class);
messagingTemplate.convertAndSend("/sub/message", chatMessage);
// messagingTemplate.convertAndSendToUser(chatMessage.getSessionId(), "/sub/message", chatMessage);
System.out.println("chatMessage = " + chatMessage);
messagingTemplate.convertAndSend("/sub/message/" + chatMessage.getId(), chatMessage);
messagingTemplate.convertAndSendToUser(chatMessage.getSessionId(), "/sub/message", chatMessage);
} catch (Exception e) {
log.error("Subscriber Error", e);
}