728x90
[ ๊ฐ๋ฐ ๋ถ๋ถ ]
WebSocketHandler.java
package egovframework...;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import egovframework.pcr.main.web.controller.AdminController;
@Repository
public class WebSocketHandler extends TextWebSocketHandler {
@Autowired
SqlSession sqlsession;
private static final Logger logger = LoggerFactory.getLogger(AdminController.class);
private Map<String, WebSocketSession> users = new ConcurrentHashMap<>();
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception{
logger.debug(session.getId() + " ์ฐ๊ฒฐ ๋จ");
users.put(session.getId(), session);
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
String msg = message.getPayload();
// js์์ ๋ณด๋ธ ์ธ์
์์ด๋ ๊ฐ
if(StringUtils.isNotEmpty(msg)) {
String sendId = msg;
// ํ์ฌ session์ ๋ด๊ฒจ์๋ ๋ชจ๋ ์ฌ์ฉ์๋ฅผ ์ฒดํฌํ๊ธฐ ์ํจ.
for (WebSocketSession responseIdSession : users.values()) {
if (responseIdSession != null) {
TextMessage tmpMsg = new TextMessage(sendId);
responseIdSession.sendMessage(tmpMsg);
}
}
}
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception{
logger.debug(session.getId() + " ์ฐ๊ฒฐ ์ข
๋ฃ๋จ");
users.remove(session.getId());
}
}
js
<script>
var socket = null;
connect();
function connect() {
// egov-com-sevlet.xml์์ mapping path์ ๊ฑธ๋ ค ์น์์ผ ํธ๋ค๋ฌ๊ฐ ์์ฒญ์ ์ฒ๋ฆฌ
var ws = new WebSocket('ws://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/websocket.do');
socket = ws;
// ์ฐ๊ฒฐ ์ฑ๊ณต์
ws.onopen = function() {
console.log('Info: connection opened.');
};
// ์๋ต ๋ฉ์ธ์ง ์์ง ๋ถ๋ถ
ws.onmessage = function(event) {
var gUserId = $("#userSessionId").val();
var sm = event.data;
// ๋ก๊ทธ์ธํ ์ฌ์ฉ์์๊ฒ ์๋ฆผ์ด ๊ฐ์ง์๋๋ก ํ๊ธฐ ์ํจ.
if(sm != gUserId){
var websocketQna = document.getElementById("websocketQna");
websocketQna.style.display = "block";
// setTimeout์ ์ฃผ์ด 3์ด๋ง ํ๋ฉด์ ์ถ๋ ฅ
setTimeout(function(){
websocketQna.style.display = "none";
}, 3000); //3000 : 3์ด
}
};
// ์ฐ๊ฒฐ ์ข
๋ฃ์
ws.onclose = function(event) {
console.log('Info: connection closed');
};
// ์๋ฌ ๋ฐ์์
ws.onerror = function(err) {
console.log('Error:', err);
};
}
// ์ ์ผ ๋จผ์ ์คํ ๋๋ ๋ถ๋ถ.
$(document).ready(function() {
// ๋ฒํผ์ ํด๋ฆญ ์ ์ด๋ฒคํธ
$('#alertQnaOk').on('click', function(evt) {
// ํ์ฌ ๋ก๊ทธ์ธ ํ sessionId๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํจ.
var gUserId = $("#userSessionId").val();
// form ์์ ์๋ input ๋ฑ ์ ์กํ ์ ์๋ ๋์์ ์ค๋จ
evt.preventDefault();
// readyState 1์ผ๋ webSocket๊ฐ์ฒด ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํจ๋ค.
if (socket.readyState !== 1)
return;
// ์ธ์
๊ฐ์ ๋ณด๋ธ๋ค.
socket.send(gUserId);
$('#alertQna').css('display', 'none');
});
socket.onclose();
});
</script>
html
<div class="alret QnA" id="websocketQna" style="top:-10px;left: 1345px;">
<h4>์๋ก์ด Q&A๊ฐ ๋ฑ๋ก๋์์ต๋๋ค.</h4>
<div class="btn-box">
</div>
</div>
์ถ์ฒ ๊ธ์ด insert ๋ ๋ websocket์ ๋ฐฑ๋จ์์ ํธ์ถํด ์ค์๊ฐ ์๋ฆผ์ ๊ตฌํ์ ์๋ ํด๋ฆญ