728x90
Q&A๋ฟ๋ง์๋๋ผ ๊ณต์ง์ฌํญ์ด๋ ๋ค๋ฅธ ๋ถ๋ถ์์๋ ๊ธ์ด insert ๋ ๋
WebSocket์ ๋ฐฑ๋จ์์ ํธ์ถํด ์ค์๊ฐ ์๋ฆผ์ ๊ตฌํํด๋ณด์๋ค.
[ ๊ฐ๋ฐ ๋ถ๋ถ ]
1. ๋ฉ์๋๊ฐ ์๋์ผ๋ก ํธ์ถ๋๊ณ , ์ธ์คํด์ค๊ฐ ์๋์ผ๋ก ์ฃผ์
์ฌ์ฉํ controller ์ ์ถ๊ฐ (์ : boardController.java)
@Autowired
private WebSocketHandler websocketHandler;
2. handleTextMessage ์ด๋
WebSocketHandler.java ๋ถ๋ถ์ handleTextMessage๋ฅผ ์์ ์ด ์ฌ์ฉํ๋ (์ : boardController.java)๋ก ์ด๋.
public void boardSocket(String who,LoginVO loginVO) throws Exception{
Map<String, WebSocketSession> users = websocketHandler.getUserMap();
for (WebSocketSession responseIdSession : users.values()) {
if (responseIdSession != null) {
//JSON.parse๋ก ๋๊ธธ๋
TextMessage tmpMsg = new TextMessage("{\"code\" : \""+ who +"\", \"id\" : \""+ loginVO.getId() +"\"}");
//JSON.stringify๋ก ๋๊ธธ๋
//TextMessage tmpMsg = new TextMessage(who);
responseIdSession.sendMessage(tmpMsg);
}
}
}
//์์
public void broadCast(String who, LoginVO loginVO) throws Exception{
Map<String, WebSocketSession> users = websocketHandler.getUserMap();
for (WebSocketSession responseIdSession : users.values()) {
Map<String,Object> map = responseIdSession.getAttributes();
LoginVO vo = (LoginVO) map.get("LoginResultVO");
if (responseIdSession != null) {
if(vo.getId() != loginVO.getId()){
TextMessage tmpMsg = new TextMessage("{\"code\" : \""+ who +"\"}");
//TextMessage tmpMsg = new TextMessage("{\"code\" : \""+ who +"\", \"id\" : \""+ loginVO.getId() +"\"}");
responseIdSession.sendMessage(tmpMsg);
}
}
}
}
3. insert ๋๋ ๋ถ๋ถ์ ํจ์ ๋ฉ์๋ ํธ์ถ
boardService.insertBoard(~~);
// ๋ฉ์๋ ํธ์ถ
broadSocket("qna",loginVO);
4. ํ๋ก ํธ ๋ถ๋ถ
webSocketConnect : function(){
var socket = null;
// localhost:8082๋ฅผ ์ป๊ธฐ์ํจ
var uriGet2 = window.location.origin.substring(6);
// Path์ ์ป๊ธฐ์ํจ
var offset = location.href.indexOf(location.host)+location.host.length;
var ctxPath = location.href.substring(offset,location.href.indexOf('/',offset+1));
var ws = new WebSocket("ws://" + uriGet2 + ctxPath +"/websocket");
socket = ws;
ws.onopen = function() {
console.log('Info: connection opened.');
};
ws.onmessage = function(event) {
var msg;
try{
msg = JSON.parse(event.data);
}catch(error){
console.error(error);
}
var websocketQna = $(".websocketQna");
var li;
var userSessionId = $("#userSessionId").val();
// ์์ ์ ์ ์ธํ๊ธฐ ์ํจ => ์ปจํ๋ก๋ฌ์์ ์ํ๋๋ก ์์ ํ์.
//if(userSessionId != msg.id){
$("#boardMenuTop").css("color","#fcff3a");
switch(msg.code) { // board,noice,qna ๊ตฌ๋ถ
case "notice":
$("#noticeMenuBtn").css("color","red");
li = $('<li>์๋ก์ด ๊ณต์ง์ฌํญ ๊ธ์ด ๋ฑ๋ก๋์์ต๋๋ค.</li>');
break;
case "board":
$("#boardMenuBtn").css("color","red");
li = $('<li>์๋ก์ด ๊ฒ์ํ ๊ธ์ด ๋ฑ๋ก๋์์ต๋๋ค.</li>');
break;
case "qna":
$("#qnaMenuBtn").css("color","red");
li = $('<li>์๋ก์ด Q&A ๊ธ์ด ๋ฑ๋ก๋์์ต๋๋ค.</li>');
break;
}
//}
websocketQna.append(li);
setTimeout(function(){
$(".websocketQna").find(li).remove();
}, 3000);
};
ws.onclose = function(event) {
console.log('Info: ws connection closed');
};
ws.onerror = function(err) {
console.log('ws Error:', err);
};
},
}
JSON ๊ฐ์ฒด ์ฐจ์ด๋ ์๋์ ์ ๋ฆฌํด ๋์๋ค.