-
ํด๋ผ์ด์ธํธ(Sender)๊ฐ ๋ฉ์ธ์ง๋ฅผ ๋ณด๋ด๋ฉด STOMP ํต์ ์ผ๋ก ์๋ฒ์ ๋ฉ์ธ์ง๊ฐ ์ ๋ฌ๋๋ค.
-
Controller์ @MessageMapping์ ์ํด ๋ฉ์ธ์ง๋ฅผ ๋ฐ๋๋ค.
-
Controller์ @SendTo๋ก ํน์ topic์(/1) ๊ตฌ๋ (/room) ํ๋ ํด๋ผ์ด์ธํธ์๊ฒ ๋ฉ์ธ์ง๋ฅผ ๋ณด๋ธ๋ค.
(๊ตฌ๋ ์ /room ์ผ๋ก ๋ณด๋ฉด๋๊ณ ํน์ topic์ ์ฑํ ๋ฐฉ id์ธ /1๋ก ๋ณด๋ฉด๋๋ค. -> /room/1)
https://blog.naver.com/qjawnswkd/222283176175
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.webjars:webjars-locator-core'
implementation 'org.webjars:sockjs-client:1.0.2'
implementation 'org.webjars:stomp-websocket:2.3.3'
implementation 'org.webjars:bootstrap:3.3.7'
implementation 'org.webjars:jquery:3.1.1-1'
@Configuration
@RequiredArgsConstructor
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/send"); //ํด๋ผ์ด์ธํธ์์ ๋ณด๋ธ ๋ฉ์ธ์ง๋ฅผ ๋ฐ์ prefix
registry.enableSimpleBroker("/room"); //ํด๋น ์ฃผ์๋ฅผ ๊ตฌ๋
ํ๊ณ ์๋ ํด๋ผ์ด์ธํธ๋ค์๊ฒ ๋ฉ์ธ์ง ์ ๋ฌ
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws-stomp") //SockJS ์ฐ๊ฒฐ ์ฃผ์
.withSockJS(); //๋ฒ์ ๋ฎ์ ๋ธ๋ผ์ฐ์ ์์๋ ์ ์ฉ ๊ฐ๋ฅ
// ์ฃผ์ : ws://localhost:8080/ws-stomp
}
}
@Controller
@RequiredArgsConstructor
public class ChatController {
private final ChatService chatService;
@MessageMapping("/{roomId}") //์ฌ๊ธฐ๋ก ์ ์ก๋๋ฉด ๋ฉ์๋ ํธ์ถ -> WebSocketConfig prefixes ์์ ์ ์ฉํ๊ฑด ์์ ์๋ต
@SendTo("/room/{roomId}") //๊ตฌ๋
ํ๊ณ ์๋ ์ฅ์๋ก ๋ฉ์์ง ์ ์ก (๋ชฉ์ ์ง) -> WebSocketConfig Broker ์์ ์ ์ฉํ๊ฑด ์์ ๋ถ์ด์ค์ผ๋จ
public ChatMessage test(@DestinationVariable Long roomId, ChatMessage message) {
//์ฑํ
์ ์ฅ
Chat chat = chatService.createChat(roomId, message.getSender(), message.getMessage());
return ChatMessage.builder()
.roomId(roomId)
.sender(chat.getSender())
.message(chat.getMessage())
.build();
}
}
<script th:inline="javascript">
var stompClient = null;
var roomId = [[${roomId}]];
var chatList = [[${chatList}]];
function setConnected(connected) {
$("#connect").prop("disabled", connected);
$("#disconnect").prop("disabled", !connected);
if (connected) {
$("#conversation").show();
}
else {
$("#conversation").hide();
}
$("#chatting").html("");
}
function connect() {
var socket = new SockJS('/ws-stomp');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
setConnected(true);
console.log('Connected: ' + frame);
loadChat(chatList) //์ ์ฅ๋ ์ฑํ
๋ถ๋ฌ์ค๊ธฐ
//๊ตฌ๋
stompClient.subscribe('/room/'+roomId, function (chatMessage) {
showChat(JSON.parse(chatMessage.body));
});
});
}
function disconnect() {
if (stompClient !== null) {
stompClient.disconnect();
}
setConnected(false);
console.log("Disconnected");
}
//html ์์ ์
๋ ฅ๊ฐ, roomId ๋ฅผ ๋ฐ์์ Controller ๋ก ์ ๋ฌ
function sendChat() {
stompClient.send("/send/"+roomId, {},
JSON.stringify({
'sender': $("#sender").val(),
'message' : $("#message").val()
}));
}
//์ ์ฅ๋ ์ฑํ
๋ถ๋ฌ์ค๊ธฐ
function loadChat(chatList){
if(chatList != null) {
for(chat in chatList) {
$("#chatting").append(
"<tr><td>" + "[" + chatList[chat].sender + "]" + chatList[chat].message + "</td></tr>"
);
}
}
}
//๋ณด๋ธ ์ฑํ
๋ณด๊ธฐ
function showChat(chatMessage) {
$("#chatting").append(
"<tr><td>" + "[" + chatMessage.sender + "]" + chatMessage.message + "</td></tr>"
);
}
$(function () {
$("form").on('submit', function (e) {
e.preventDefault();
});
$( "#connect" ).click(function() { connect(); });
$( "#disconnect" ).click(function() { disconnect(); });
$( "#send" ).click(function() { sendChat(); });
});
</script>
<script>
//์ฐฝ ํค๋ฉด ๋ฐ๋ก ์ฐ๊ฒฐ
window.onload = function (){
connect();
}
window.BeforeUnloadEvent = function (){
disconnect();
}
</script>
์ฑํ ๋ก๊ทธ Redis์ ์ ์ฅ