blog: chatting - connection test
This commit is contained in:
62
blog/chatting/server/socket.html
Normal file
62
blog/chatting/server/socket.html
Normal 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>
|
||||||
@@ -15,4 +15,5 @@ public class ChatMessage {
|
|||||||
private String message;
|
private String message;
|
||||||
private LocalDateTime date;
|
private LocalDateTime date;
|
||||||
private TestEnum testEnum;
|
private TestEnum testEnum;
|
||||||
|
private String id;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ import java.io.IOException;
|
|||||||
public class CorsFilter implements Filter {
|
public class CorsFilter implements Filter {
|
||||||
|
|
||||||
private final String[] allowOriginDomains = {
|
private final String[] allowOriginDomains = {
|
||||||
"http://localhost:5500",
|
"http://localhost:63342",
|
||||||
"http://127.0.0.1:5500"
|
"http://127.0.0.1:63342"
|
||||||
};
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -20,8 +20,9 @@ public class RedisSubscriber {
|
|||||||
public void sendMessage(String message) {
|
public void sendMessage(String message) {
|
||||||
try {
|
try {
|
||||||
ChatMessage chatMessage = objectMapper.readValue(message, ChatMessage.class);
|
ChatMessage chatMessage = objectMapper.readValue(message, ChatMessage.class);
|
||||||
messagingTemplate.convertAndSend("/sub/message", chatMessage);
|
System.out.println("chatMessage = " + chatMessage);
|
||||||
// messagingTemplate.convertAndSendToUser(chatMessage.getSessionId(), "/sub/message", chatMessage);
|
messagingTemplate.convertAndSend("/sub/message/" + chatMessage.getId(), chatMessage);
|
||||||
|
messagingTemplate.convertAndSendToUser(chatMessage.getSessionId(), "/sub/message", chatMessage);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Subscriber Error", e);
|
log.error("Subscriber Error", e);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user