Skip to content

Commit 5cb2ac4

Browse files
committed
feat : get attachment list by socket
1 parent 58888ab commit 5cb2ac4

File tree

6 files changed

+104
-1
lines changed

6 files changed

+104
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.bssm.attachit.domain.attachment.exception;
2+
3+
import org.bssm.attachit.global.error.exception.AttachItException;
4+
import org.bssm.attachit.global.error.exception.ErrorCode;
5+
6+
public class SocketIOException extends AttachItException {
7+
public static final SocketIOException EXCEPTION = new SocketIOException(ErrorCode.SOCKET_OUTPUT);
8+
public SocketIOException(ErrorCode errorCode) {
9+
super(errorCode);
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.bssm.attachit.domain.attachment.service;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.bssm.attachit.domain.attachment.domain.Attachment;
5+
import org.bssm.attachit.domain.attachment.repository.AttachmentRepository;
6+
import org.springframework.stereotype.Service;
7+
8+
import java.util.List;
9+
10+
@Service
11+
@RequiredArgsConstructor
12+
public class GetAttachmentListService {
13+
14+
private final AttachmentRepository attachmentRepository;
15+
16+
public List<Attachment> execute() {
17+
return attachmentRepository.findAll();
18+
}
19+
}

src/main/java/org/bssm/attachit/global/error/exception/ErrorCode.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public enum ErrorCode {
1818
FILE_ERROR(500, "파일 오류"),
1919

2020
UNAUTHORIZED(403, "권한이 올바르지 않습니다"),
21-
BAD_REQUEST(400, "잘못된 요청입니다");
21+
BAD_REQUEST(400, "잘못된 요청입니다"),
22+
23+
SOCKET_OUTPUT(500, "소켓 데이터 전송 에러");
2224

2325
private final int status;
2426
private final String message;

src/main/java/org/bssm/attachit/global/security/SecurityConfig.java

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.bssm.attachit.global.jwt.util.JwtUtil;
77
import org.springframework.context.annotation.Bean;
88
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.scheduling.annotation.EnableScheduling;
910
import org.springframework.security.config.Customizer;
1011
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
1112
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -20,6 +21,7 @@
2021
@Configuration
2122
@EnableWebSecurity
2223
@RequiredArgsConstructor
24+
@EnableScheduling
2325
public class SecurityConfig {
2426

2527
private final JwtUtil jwtUtil;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.bssm.attachit.global.security.socket;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.socket.config.annotation.*;
6+
7+
@Configuration
8+
@EnableWebSocket
9+
@RequiredArgsConstructor
10+
public class WebSocketConfig implements WebSocketConfigurer {
11+
12+
private final WebSocketHandler webSocketHandler;
13+
14+
@Override
15+
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
16+
registry.addHandler(webSocketHandler, "/attachments").setAllowedOrigins("*");
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.bssm.attachit.global.security.socket;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import lombok.RequiredArgsConstructor;
6+
import org.bssm.attachit.domain.attachment.domain.Attachment;
7+
import org.bssm.attachit.domain.attachment.exception.SocketIOException;
8+
import org.bssm.attachit.domain.attachment.service.GetAttachmentListService;
9+
import org.springframework.scheduling.annotation.Scheduled;
10+
import org.springframework.stereotype.Component;
11+
import org.springframework.web.socket.CloseStatus;
12+
import org.springframework.web.socket.TextMessage;
13+
import org.springframework.web.socket.WebSocketSession;
14+
import org.springframework.web.socket.handler.TextWebSocketHandler;
15+
16+
import java.io.IOException;
17+
import java.util.List;
18+
import java.util.concurrent.ConcurrentHashMap;
19+
20+
@Component
21+
@RequiredArgsConstructor
22+
public class WebSocketHandler extends TextWebSocketHandler {
23+
24+
private final GetAttachmentListService getAttachmentListService;
25+
private static final ObjectMapper objectMapper = new ObjectMapper();
26+
private static final ConcurrentHashMap<String, WebSocketSession> CLIENTS = new ConcurrentHashMap<>();
27+
28+
@Override
29+
public void afterConnectionEstablished(WebSocketSession session) throws RuntimeException {
30+
CLIENTS.put(session.getId(), session);
31+
}
32+
33+
@Override
34+
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws RuntimeException {
35+
CLIENTS.remove(session.getId());
36+
}
37+
38+
@Scheduled(fixedRate = 1000, initialDelay = 5000)
39+
public void sendAttachmentListToAllClients() throws JsonProcessingException {
40+
List<Attachment> attachmentList = getAttachmentListService.execute();
41+
String jsonAttachments = objectMapper.writeValueAsString(attachmentList);
42+
TextMessage message = new TextMessage(jsonAttachments);
43+
CLIENTS.forEach((key, value) -> {
44+
try {
45+
value.sendMessage(message);
46+
} catch (IOException e) {
47+
throw SocketIOException.EXCEPTION;
48+
}
49+
});
50+
}
51+
}

0 commit comments

Comments
 (0)