back 코드
WebSocketConfig
package com.ssafy.common.exception.handler;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
private final WebSocketHandler webSocketHandler;
public WebSocketConfig(WebSocketHandler webSocketHandler) {
this.webSocketHandler = webSocketHandler;
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(webSocketHandler, "/ssen")
.setAllowedOriginPatterns("*")
.addInterceptors(new HttpSessionHandshakeInterceptor());
}
}
WebSocketHandler
package com.ssafy.common.exception.handler;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.ssafy.api.response.LiveCurrencyRes;
import com.ssafy.api.response.LiveUserRes;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
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 java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Component
public class WebSocketHandler extends TextWebSocketHandler {
private static final ConcurrentHashMap<String, WebSocketSession> CLIENTS = new ConcurrentHashMap<String, WebSocketSession>();
Gson gson = new Gson();
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
CLIENTS.put(session.getId(), session);
}
public ConcurrentHashMap getWebsocketSession() {
return CLIENTS;
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
CLIENTS.remove(session.getId());
}
public void handleTextMessage(LiveUserRes lurList) {
CLIENTS.entrySet().forEach(arg -> {
try {
arg.getValue().sendMessage(new TextMessage(gson.toJson(lurList)));
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
front 코드
웹소켓 연결, 연결 끊기, 메세지 출력
// 웹소켓 연결
const webSocket = new WebSocket("ws://localhost:8080/ssen");
useEffect(()=> {
webSocket.onopen = function(){
console.log("서버와 웹소켓 연결 성공");
};
}, []);
const logoClickHandler = () => {
webSocket.close();
console.log("서버와 웹소켓 연결 끊기");
navigate("/");
};
webSocket.onmessage = function(message) {
console.log(JSON.parse(message.data));
}
로컬(백, 프론트 둘 다 로컬) 에서는 아래 코드로 연결 잘 됩니다.
const webSocket = new WebSocket("ws://localhost:8080/ssen");서버에 배포 시키고 나서는 안됩니다.

const webSocket = new WebSocket("wss://j7e204.p.ssafy.io:8080/ssen");const webSocket = new WebSocket("ws://j7e204.p.ssafy.io:8080/ssen");const webSocket = new WebSocket("wss://j7e204.p.ssafy.io/ssen");const webSocket = new WebSocket("wss://localhost:8080/ssen");const webSocket = new WebSocket("ws://localhost:8080/ssen");const webSocket = new WebSocket("ws://j7e204.p.ssafy.io:8080/ssen");