<!-- 웹소켓 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>${org.springframework-version}</version>
</dependency>
xmlns:websocket="http://www.springframework.org/schema/websocket"
추가http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.3.xsd
추가<!-- WebSocket -->
<beans:bean id="webSocketHandler" class="com.common.websocket.WebSocketHandler" />
<websocket:handlers>
<websocket:mapping handler="webSocketHandler" path="/websocket"/>
<websocket:handshake-interceptors>
<beans:bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
</websocket:handshake-interceptors>
</websocket:handlers>
servlet 태그 내부에 <async-supported>true</async-supported>
를 추가해준다. 다수의 클라이언트들이 동시에 정보를 보내기 위한 설정이다.
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/config/context/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
package com.common.websocket;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import com.april.app.biz.frnt.FrontDAO;
import com.common.session.login.std.StandardAuthSuccessHandler;
public class WebSocketHandler {
private static final Logger log = LoggerFactory.getLogger(StandardAuthSuccessHandler.class);
@Resource(name = "FrontDAO")
private FrontDAO FrontDAO;
@Autowired
public void setAlarmDAO(FrontDAO FrontDAO) {
this.FrontDAO = FrontDAO;
}
//전체 세션
private static List<WebSocketSession> sessions = new ArrayList<WebSocketSession>();
//개인
// private Map<String, WebSocketSession> userSessionsMap = new HashMap<String, WebSocketSession>();
//클라이언트가 서버에 접속 성공시 호출
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
sessions.add(session); //리스트에 접속한 session들을 담음
}
//소켓에 메세지를 보냈을 때 호출
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
for(WebSocketSession single : sessions) {
// String memId = message.getPayload();
Map<String, Object> count = FrontDAO.selectStatusCountInfo();
int newCount = Integer.parseInt(count.toString());
//newCount가 0이 아닐 경우 메세지 전송
if(newCount != 0) {
TextMessage sendMsg = new TextMessage(newCount + "개");
single.sendMessage(sendMsg);
}
}
}
//연결이 종료됐을 때
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
//전체 세션리스트에서 세션 삭제
sessions.remove(session);
}
}
window.onload = function(){
let webSocket = new WebSocket("ws://localhost");
webSocket.onopen = () => {
webSocket.send();
wsSend();
}
webSocket.onmessage = (e) => {
// console.log("핸들러에서 전송한 메세지",e.data);
var status = document.querySelector(".status");
console.log(status, '-> status');
staus.innerText = e.data;
}
var wsSend=()=>{
setInterval(function() {
// 3초마다 클라이언트로 메시지 전송
webSocket.send();
}, 3000);
}
}
https://bmangrok.tistory.com/entry/WebSocket-%EC%8A%A4%ED%94%84%EB%A7%81-%EC%B1%84%ED%8C%85-%EA%B5%AC%ED%98%84-1
https://admm.tistory.com/81
https://simplehanlab.github.io/add-websocket
https://velog.io/@rim/%EC%9B%B9%EC%86%8C%EC%BC%93%EC%9C%BC%EB%A1%9C-%EC%8B%A4%EC%8B%9C%EA%B0%84-%EC%95%8C%EB%A6%BC-%EB%A7%8C%EB%93%A4%EA%B8%B01
https://hoon2kc.tistory.com/entry/websocket-%EC%8B%A4%EC%8B%9C%EA%B0%84-%EC%95%8C%EB%A6%BC